Skip to content

Commit 75f2267

Browse files
committed
uapi: Generate bindings for all architectures
Until now, we only used the x86_64 architecture, but here are the changes to use this crate on any architecture. Add the bindgen.sh script to make update simple: ./src/uapi/bindgen.sh .../linux-6.12 This call creates three binding files: - landlock_x86_64.rs - landlock_i686.rs - landlock_all.rs Only landlock_all.rs is generated without memory layout tests to make it architecture-agnostic. For instance, x86_64-linux-gnu (64-bit), aarch64-unknown-linux-gnu (64-bit), and armv7-unknown-linux-gnueabi (32-bit) all align memory on 8 bytes, which is not the case for i686-linux-gnu (32-bit). This makes i686 tests incompatible for bindgen_test_layout_landlock_ruleset_attr() and bindgen_test_layout_landlock_net_port_attr(). Extend the CI to build and test on i686. Closes: #110 Signed-off-by: Mickaël Salaün <mic@digikod.net>
1 parent 229f152 commit 75f2267

File tree

7 files changed

+394
-252
lines changed

7 files changed

+394
-252
lines changed

.github/workflows/rust.yml

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -131,21 +131,31 @@ jobs:
131131
- name: Get MSRV
132132
run: sed -n 's/^rust-version = "\([0-9.]\+\)"$/RUST_TOOLCHAIN=\1/p' Cargo.toml >> $GITHUB_ENV
133133

134+
- name: Install 32-bit development libraries
135+
run: |
136+
sudo apt update
137+
sudo apt install gcc-multilib libc6-dev-i386
138+
134139
- name: Install Rust MSRV
135140
run: |
136141
rm ~/.cargo/bin/{cargo-fmt,rustfmt} || :
137142
rustup self update
138143
rustup default ${{ env.RUST_TOOLCHAIN }}
139144
rustup update ${{ env.RUST_TOOLCHAIN }}
145+
rustup target add i686-unknown-linux-gnu
146+
rustup target add x86_64-unknown-linux-gnu
140147
141-
- name: Build
142-
run: rustup run ${{ env.RUST_TOOLCHAIN }} cargo build --verbose
148+
- name: Build (x86_64)
149+
run: rustup run ${{ env.RUST_TOOLCHAIN }} cargo build --target x86_64-unknown-linux-gnu --verbose
143150

144-
- name: Build tests
145-
run: rustup run ${{ env.RUST_TOOLCHAIN }} cargo build --tests --verbose
151+
- name: Build (x86)
152+
run: rustup run ${{ env.RUST_TOOLCHAIN }} cargo build --target i686-unknown-linux-gnu --verbose
146153

147-
- name: Run tests against the local kernel (Landlock ABI ${{ env.LANDLOCK_CRATE_TEST_ABI }})
148-
run: rustup run ${{ env.RUST_TOOLCHAIN }} cargo test --verbose
154+
- name: Run tests against the local kernel (Landlock ABI ${{ env.LANDLOCK_CRATE_TEST_ABI }} on x86_64)
155+
run: rustup run ${{ env.RUST_TOOLCHAIN }} cargo test --target x86_64-unknown-linux-gnu --verbose
156+
157+
- name: Run tests against the local kernel (Landlock ABI ${{ env.LANDLOCK_CRATE_TEST_ABI }} on x86)
158+
run: rustup run ${{ env.RUST_TOOLCHAIN }} cargo test --target i686-unknown-linux-gnu --verbose
149159

150160
- name: Run tests against Linux 6.1
151161
run: CARGO="rustup run ${{ env.RUST_TOOLCHAIN }} cargo" ./landlock-test-tools/test-rust.sh linux-6.1 2

src/uapi/bindgen.sh

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#!/usr/bin/env bash
2+
# SPDX-License-Identifier: Apache-2.0 OR MIT
3+
4+
set -u -e -o pipefail
5+
6+
if [[ $# -ne 1 ]]; then
7+
echo "usage $(basename -- "${BASH_SOURCE[0]}") <kernel-source>" >&2
8+
exit 1
9+
fi
10+
11+
HEADER="$(readlink -f -- "$1")/include/uapi/linux/landlock.h"
12+
13+
if [[ ! -f "${HEADER}" ]]; then
14+
echo "File not found: ${HEADER}" >&2
15+
exit 1
16+
fi
17+
18+
cd "$(dirname "${BASH_SOURCE[0]}")"
19+
20+
MSRV="$(sed -n 's/^rust-version = "\(.*\)"/\1/p' ../../Cargo.toml)"
21+
22+
bindgen_landlock() {
23+
local arch="$1"
24+
local output="$2"
25+
shift 2
26+
27+
bindgen \
28+
"$@" \
29+
--rust-target "${MSRV}" \
30+
--ctypes-prefix="::std::os::raw" \
31+
--allowlist-type "landlock_.*" \
32+
--allowlist-var "LANDLOCK_.*" \
33+
--no-doc-comments \
34+
--no-derive-default \
35+
--output "${output}" \
36+
"${HEADER}" \
37+
-- \
38+
--target="${arch}-linux-gnu"
39+
}
40+
41+
for ARCH in x86_64 i686; do
42+
echo "Generating bindings with tests for ${ARCH}."
43+
bindgen_landlock "${ARCH}" "landlock_${ARCH}.rs"
44+
done
45+
46+
# The Landlock ABI is architecture-agnostic (except for std::os::raw and memory
47+
# alignment).
48+
echo "Generating bindings without tests."
49+
bindgen_landlock x86_64 "landlock_all.rs" --no-layout-tests

src/uapi/landlock.rs

Lines changed: 0 additions & 246 deletions
This file was deleted.

src/uapi/landlock_all.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/* automatically generated by rust-bindgen 0.72.0 */
2+
3+
pub const LANDLOCK_CREATE_RULESET_VERSION: u32 = 1;
4+
pub const LANDLOCK_ACCESS_FS_EXECUTE: u32 = 1;
5+
pub const LANDLOCK_ACCESS_FS_WRITE_FILE: u32 = 2;
6+
pub const LANDLOCK_ACCESS_FS_READ_FILE: u32 = 4;
7+
pub const LANDLOCK_ACCESS_FS_READ_DIR: u32 = 8;
8+
pub const LANDLOCK_ACCESS_FS_REMOVE_DIR: u32 = 16;
9+
pub const LANDLOCK_ACCESS_FS_REMOVE_FILE: u32 = 32;
10+
pub const LANDLOCK_ACCESS_FS_MAKE_CHAR: u32 = 64;
11+
pub const LANDLOCK_ACCESS_FS_MAKE_DIR: u32 = 128;
12+
pub const LANDLOCK_ACCESS_FS_MAKE_REG: u32 = 256;
13+
pub const LANDLOCK_ACCESS_FS_MAKE_SOCK: u32 = 512;
14+
pub const LANDLOCK_ACCESS_FS_MAKE_FIFO: u32 = 1024;
15+
pub const LANDLOCK_ACCESS_FS_MAKE_BLOCK: u32 = 2048;
16+
pub const LANDLOCK_ACCESS_FS_MAKE_SYM: u32 = 4096;
17+
pub const LANDLOCK_ACCESS_FS_REFER: u32 = 8192;
18+
pub const LANDLOCK_ACCESS_FS_TRUNCATE: u32 = 16384;
19+
pub const LANDLOCK_ACCESS_FS_IOCTL_DEV: u32 = 32768;
20+
pub const LANDLOCK_ACCESS_NET_BIND_TCP: u32 = 1;
21+
pub const LANDLOCK_ACCESS_NET_CONNECT_TCP: u32 = 2;
22+
pub const LANDLOCK_SCOPE_ABSTRACT_UNIX_SOCKET: u32 = 1;
23+
pub const LANDLOCK_SCOPE_SIGNAL: u32 = 2;
24+
pub type __s32 = ::std::os::raw::c_int;
25+
pub type __u64 = ::std::os::raw::c_ulonglong;
26+
#[repr(C)]
27+
#[derive(Debug, Copy, Clone)]
28+
pub struct landlock_ruleset_attr {
29+
pub handled_access_fs: __u64,
30+
pub handled_access_net: __u64,
31+
pub scoped: __u64,
32+
}
33+
pub const landlock_rule_type_LANDLOCK_RULE_PATH_BENEATH: landlock_rule_type = 1;
34+
pub const landlock_rule_type_LANDLOCK_RULE_NET_PORT: landlock_rule_type = 2;
35+
pub type landlock_rule_type = ::std::os::raw::c_uint;
36+
#[repr(C, packed)]
37+
#[derive(Debug, Copy, Clone)]
38+
pub struct landlock_path_beneath_attr {
39+
pub allowed_access: __u64,
40+
pub parent_fd: __s32,
41+
}
42+
#[repr(C)]
43+
#[derive(Debug, Copy, Clone)]
44+
pub struct landlock_net_port_attr {
45+
pub allowed_access: __u64,
46+
pub port: __u64,
47+
}

0 commit comments

Comments
 (0)