Skip to content

Commit 68b24a9

Browse files
authored
Add module nat20device to linux examples. (#99)
This module creates a new character device class intended to implement the nat20 service protocol implementing DICE based device state attestation and an embedded CA.
1 parent a7286d9 commit 68b24a9

10 files changed

Lines changed: 785 additions & 1 deletion

File tree

.github/workflows/linux-kmod-build.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,17 @@ jobs:
9292
find ${{ runner.temp }}/buildroot.build -name 'nat20lib.ko' | grep -q nat20lib.ko
9393
echo "nat20lib.ko built successfully:"
9494
find ${{ runner.temp }}/buildroot.build -name 'nat20lib.ko' -exec ls -la {} \;
95+
96+
- name: Build nat20device kernel module
97+
env:
98+
NAT20DEVICE_OVERRIDE_SRCDIR: ${{ github.workspace }}
99+
run: |
100+
cd ${{ runner.temp }}/buildroot.build/buildroot
101+
make nat20device-dirclean
102+
make nat20device -j $(( $(nproc) + 1 ))
103+
104+
- name: Verify nat20device.ko was produced
105+
run: |
106+
find ${{ runner.temp }}/buildroot.build -name 'nat20device.ko' | grep -q nat20device.ko
107+
echo "nat20device.ko built successfully:"
108+
find ${{ runner.temp }}/buildroot.build -name 'nat20device.ko' -exec ls -la {} \;

examples/linux/br_external/Config.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,5 @@
3333
# along with this program; if not, see
3434
# <https://www.gnu.org/licenses/>.
3535

36+
source "$BR2_EXTERNAL_NAT20_PATH/package/nat20device/Config.in"
3637
source "$BR2_EXTERNAL_NAT20_PATH/package/nat20lib/Config.in"

examples/linux/br_external/configs/qemu_br_defconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3976,4 +3976,5 @@ BR2_TARGET_UBOOT_CUSTOM_PATCH_DIR=""
39763976
#
39773977
# Provides NAT20 related packages.
39783978
#
3979+
BR2_PACKAGE_NAT20DEVICE=y
39793980
BR2_PACKAGE_NAT20LIB=y
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# Copyright 2026 Aurora Operations, Inc.
2+
#
3+
# SPDX-License-Identifier: Apache-2.0 OR GPL-2.0
4+
#
5+
# This work is dual licensed.
6+
# You may use it under Apache-2.0 or GPL-2.0 at your option.
7+
#
8+
# Licensed under the Apache License, Version 2.0 (the "License");
9+
# you may not use this file except in compliance with the License.
10+
# You may obtain a copy of the License at
11+
#
12+
# http://www.apache.org/licenses/LICENSE-2.0
13+
#
14+
# Unless required by applicable law or agreed to in writing, software
15+
# distributed under the License is distributed on an "AS IS" BASIS,
16+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
# See the License for the specific language governing permissions and
18+
# limitations under the License.
19+
#
20+
# OR
21+
#
22+
# This program is free software; you can redistribute it and/or
23+
# modify it under the terms of the GNU General Public License
24+
# as published by the Free Software Foundation; either version 2
25+
# of the License, or (at your option) any later version.
26+
#
27+
# This program is distributed in the hope that it will be useful,
28+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
29+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30+
# GNU General Public License for more details.
31+
#
32+
# You should have received a copy of the GNU General Public License
33+
# along with this program; if not, see
34+
# <https://www.gnu.org/licenses/>.
35+
36+
config BR2_PACKAGE_NAT20DEVICE
37+
bool "nat20device"
38+
help
39+
Say Y if you want to enable libnat20 based DICE support.
40+
41+
Congratulations, you rolled a natural 20. You just enabled
42+
DICE (Device Identifier Composition Engine) support which
43+
is one step in the direction of establishing the root of
44+
trust for your ZTA infrastructure.
45+
46+
To compile this driver as a module, choose M here: the
47+
module will be called nat20device.
48+
49+
If in doubt, read up on ZTA, device state attestation, OpenDICE,
50+
DICE Attestation Architecture, and DICE Layering Architecture.
51+
Then come back and, enthusiastically, say Y here.
52+
53+
Privacy notice: This is a trusted computing feature. It is
54+
very useful for fleet management and establishing authenticity
55+
and integrity to a remote relying party. It can also be
56+
used for tracking, so protecting this feature from unauthorized
57+
access is crucial for privacy on personal end user devices.
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Copyright 2026 Aurora Operations, Inc.
2+
#
3+
# SPDX-License-Identifier: Apache-2.0 OR GPL-2.0
4+
#
5+
# This work is dual licensed.
6+
# You may use it under Apache-2.0 or GPL-2.0 at your option.
7+
#
8+
# Licensed under the Apache License, Version 2.0 (the "License");
9+
# you may not use this file except in compliance with the License.
10+
# You may obtain a copy of the License at
11+
#
12+
# http://www.apache.org/licenses/LICENSE-2.0
13+
#
14+
# Unless required by applicable law or agreed to in writing, software
15+
# distributed under the License is distributed on an "AS IS" BASIS,
16+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
# See the License for the specific language governing permissions and
18+
# limitations under the License.
19+
#
20+
# OR
21+
#
22+
# This program is free software; you can redistribute it and/or
23+
# modify it under the terms of the GNU General Public License
24+
# as published by the Free Software Foundation; either version 2
25+
# of the License, or (at your option) any later version.
26+
#
27+
# This program is distributed in the hope that it will be useful,
28+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
29+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30+
# GNU General Public License for more details.
31+
#
32+
# You should have received a copy of the GNU General Public License
33+
# along with this program; if not, see
34+
# <https://www.gnu.org/licenses/>.
35+
36+
# In CI NAT20DEVICE_OVERRIDE_SRCDIR is set to the root of the repository,
37+
# so that the source under test is always the current branch.
38+
# Integrators who use this configuration should pin the version
39+
# to a specific commit or branch to avoid breakages when the main branch changes.
40+
NAT20DEVICE_VERSION = origin/main
41+
NAT20DEVICE_SITE = https://github.com/aurora-opensource/libnat20.git
42+
NAT20DEVICE_SITE_METHOD = git
43+
NAT20DEVICE_LICENSE = Apache-2.0 OR GPL-2.0
44+
NAT20DEVICE_LICENSE_FILES = LICENSE-Apache-2.0.txt LICENSE-GPL-2.0.txt
45+
46+
NAT20DEVICE_MODULE_SUBDIRS = examples/linux/nat20device
47+
48+
$(eval $(kernel-module))
49+
$(eval $(generic-package))

examples/linux/br_external/utils/envsetup.sh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ fi
4646

4747
source .env
4848

49+
export NAT20DEVICE_OVERRIDE_SRCDIR="$LIBNAT20_ROOT"
4950
export NAT20LIB_OVERRIDE_SRCDIR="$LIBNAT20_ROOT"
5051

5152
function ensure_popd() {
@@ -68,14 +69,15 @@ function brrebuild() {
6869
echo "Available targets:"
6970
echo " all - Rebuild all components"
7071
echo " linux - Rebuild the linux kernel"
72+
echo " nat20device - Rebuild the nat20device module"
7173
echo " nat20lib - Rebuild the nat20lib library"
7274
popd
7375
return 1
7476
fi
7577

7678
case "$1" in
7779
all)
78-
ensure_popd make linux-rebuild nat20lib-rebuild all
80+
ensure_popd make linux-rebuild nat20device-rebuild nat20lib-rebuild all
7981
;;
8082
*)
8183
ensure_popd make $1-rebuild all

examples/linux/nat20device/Kbuild

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Copyright 2026 Aurora Operations, Inc.
2+
#
3+
# SPDX-License-Identifier: Apache-2.0 OR GPL-2.0
4+
#
5+
# This work is dual licensed.
6+
# You may use it under Apache-2.0 or GPL-2.0 at your option.
7+
#
8+
# Licensed under the Apache License, Version 2.0 (the "License");
9+
# you may not use this file except in compliance with the License.
10+
# You may obtain a copy of the License at
11+
#
12+
# http://www.apache.org/licenses/LICENSE-2.0
13+
#
14+
# Unless required by applicable law or agreed to in writing, software
15+
# distributed under the License is distributed on an "AS IS" BASIS,
16+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
# See the License for the specific language governing permissions and
18+
# limitations under the License.
19+
#
20+
# OR
21+
#
22+
# This program is free software; you can redistribute it and/or
23+
# modify it under the terms of the GNU General Public License
24+
# as published by the Free Software Foundation; either version 2
25+
# of the License, or (at your option) any later version.
26+
#
27+
# This program is distributed in the hope that it will be useful,
28+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
29+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30+
# GNU General Public License for more details.
31+
#
32+
# You should have received a copy of the GNU General Public License
33+
# along with this program; if not, see
34+
# <https://www.gnu.org/licenses/>.
35+
36+
obj-m := nat20device.o
37+
38+
ccflags-y := -I $(src)/include
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Copyright 2026 Aurora Operations, Inc.
2+
#
3+
# SPDX-License-Identifier: Apache-2.0 OR GPL-2.0
4+
#
5+
# This work is dual licensed.
6+
# You may use it under Apache-2.0 or GPL-2.0 at your option.
7+
#
8+
# Licensed under the Apache License, Version 2.0 (the "License");
9+
# you may not use this file except in compliance with the License.
10+
# You may obtain a copy of the License at
11+
#
12+
# http://www.apache.org/licenses/LICENSE-2.0
13+
#
14+
# Unless required by applicable law or agreed to in writing, software
15+
# distributed under the License is distributed on an "AS IS" BASIS,
16+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
# See the License for the specific language governing permissions and
18+
# limitations under the License.
19+
#
20+
# OR
21+
#
22+
# This program is free software; you can redistribute it and/or
23+
# modify it under the terms of the GNU General Public License
24+
# as published by the Free Software Foundation; either version 2
25+
# of the License, or (at your option) any later version.
26+
#
27+
# This program is distributed in the hope that it will be useful,
28+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
29+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30+
# GNU General Public License for more details.
31+
#
32+
# You should have received a copy of the GNU General Public License
33+
# along with this program; if not, see
34+
# <https://www.gnu.org/licenses/>.
35+
36+
KDIR ?= /lib/modules/`uname -r`/build
37+
INSTALL_MOD_PATH ?= /lib/modules/`uname -r`/extra
38+
39+
all: modules
40+
41+
modules:
42+
$(MAKE) -C $(KDIR) M=$$PWD modules
43+
44+
modules_install:
45+
$(MAKE) -C $(KDIR) M=$$PWD INSTALL_MOD_PATH="$(INSTALL_MOD_PATH)" modules_install
46+
47+
clean:
48+
$(MAKE) -C $(KDIR) M=$$PWD clean
49+
50+
.PHONY: all modules modules_install clean
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
/*
2+
* Copyright 2026 Aurora Operations, Inc.
3+
*
4+
* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0
5+
*
6+
* This work is dual licensed.
7+
* You may use it under Apache-2.0 or GPL-2.0 at your option.
8+
*
9+
* Licensed under the Apache License, Version 2.0 (the "License");
10+
* you may not use this file except in compliance with the License.
11+
* You may obtain a copy of the License at
12+
*
13+
* http://www.apache.org/licenses/LICENSE-2.0
14+
*
15+
* Unless required by applicable law or agreed to in writing, software
16+
* distributed under the License is distributed on an "AS IS" BASIS,
17+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18+
* See the License for the specific language governing permissions and
19+
* limitations under the License.
20+
*
21+
* OR
22+
*
23+
* This program is free software; you can redistribute it and/or
24+
* modify it under the terms of the GNU General Public License
25+
* as published by the Free Software Foundation; either version 2
26+
* of the License, or (at your option) any later version.
27+
*
28+
* This program is distributed in the hope that it will be useful,
29+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
30+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31+
* GNU General Public License for more details.
32+
*
33+
* You should have received a copy of the GNU General Public License
34+
* along with this program; if not, see
35+
* <https://www.gnu.org/licenses/>.
36+
*/
37+
38+
#pragma once
39+
40+
#include <linux/module.h>
41+
#include <linux/types.h>
42+
43+
struct nat20device_driver {};
44+
45+
/**
46+
* struct nat20device_buffer - Buffer for dispatch function response
47+
* @data: Pointer to buffer data
48+
* @size: Size of the buffer in bytes
49+
*/
50+
struct nat20device_buffer {
51+
void* data;
52+
size_t size;
53+
};
54+
55+
/**
56+
* typedef nat20device_dispatch_fn - Dispatch function callback
57+
* @ctx: Driver-specific context
58+
* @request: Request buffer from userspace
59+
* @request_len: Length of request buffer
60+
* @response: Pointer to response buffer (allocated by driver)
61+
*
62+
* The dispatch function processes a request and returns a response buffer.
63+
* The driver must allocate the response buffer, which will be freed by
64+
* the framework using kfree after the read operation completes,
65+
* on the next write if the buffer has not been read yet, or when the file is closed.
66+
*
67+
* The framework serializes calls per open file descriptor. However, if the
68+
* device is opened multiple times, dispatch may be called concurrently with
69+
* the same @ctx from different file descriptors. The implementer must protect
70+
* shared state in @ctx against concurrent access.
71+
*
72+
* Return: 0 on success, negative error code on failure
73+
*/
74+
typedef int (*nat20device_dispatch_fn)(void* ctx,
75+
void const* request,
76+
size_t request_len,
77+
struct nat20device_buffer* response);
78+
79+
/**
80+
* typedef nat20device_dice_chain_read - DICE chain read function callback
81+
* @ctx: Driver-specific context
82+
* @buf: User-space buffer to read DICE chain data into
83+
* @len: Length of the buffer
84+
* @f_pos: File position offset
85+
*
86+
* Reads the DICE certificate chain into the provided user-space buffer.
87+
* The data is encoded as a CBOR indefinite-length array. See
88+
* examples/linux/README.md for the encoding specification.
89+
*
90+
* This function may be called concurrently from multiple readers via the
91+
* securityfs interface. The implementer must ensure that concurrent access
92+
* to the underlying data is safe.
93+
*
94+
* Return: Number of bytes read on success, negative error code on failure
95+
*/
96+
typedef ssize_t (*nat20device_dice_chain_read)(void* ctx,
97+
char __user* buf,
98+
size_t len,
99+
loff_t* f_pos);
100+
101+
/**
102+
* struct nat20device_driver_ops - Driver operations
103+
* @dispatch: Dispatch function for handling requests
104+
* @dice_chain_read: DICE chain read function for reading the boot certificate chain
105+
*/
106+
struct nat20device_driver_ops {
107+
nat20device_dispatch_fn dispatch;
108+
nat20device_dice_chain_read dice_chain_read;
109+
};
110+
111+
/**
112+
* nat20device_register_driver - Register a new NAT20 driver instance
113+
* @ops: Driver operations structure
114+
* @ctx: Driver-specific context
115+
* @owner: Module owner (usually THIS_MODULE). This is used to manage module
116+
* reference counting for the driver instance. Blocks the removal
117+
* of the module while a device node remains open.
118+
*
119+
* Registers a new driver instance and creates a character device node
120+
* with the name "nat20X" where X is an automatically assigned number.
121+
*
122+
* Return: Pointer to registered driver on success, ERR_PTR on failure
123+
*/
124+
struct nat20device_driver* nat20device_register_driver(const struct nat20device_driver_ops* ops,
125+
void* ctx,
126+
struct module* owner);
127+
128+
/**
129+
* nat20device_unregister_driver - Unregister a NAT20 driver instance
130+
* @driver: Driver instance to unregister
131+
*
132+
* Unregisters a driver instance and removes its character device node.
133+
*
134+
* IMPORTANT:
135+
* This function must only be called from the registering module's exit
136+
* function. The file_operations.owner field is set to the registering
137+
* module, causing the kernel to hold a module reference for each open
138+
* file descriptor. This guarantees that module unload (and thus this
139+
* function) cannot execute while any file descriptor is still open.
140+
* Calling this function from any other context voids this guarantee
141+
* and results in undefined behavior.
142+
*/
143+
void nat20device_unregister_driver(struct nat20device_driver* driver);

0 commit comments

Comments
 (0)