Skip to content

Commit a370435

Browse files
committed
feat(tdx-attest): Add MUSL libc support for TDX attestation
Enable building with x86_64-unknown-linux-musl target by: 1. Vendoring minimal Linux kernel headers that are missing from MUSL: - linux/vm_sockets.h: vsock address family and sockaddr_vm struct - linux/types.h: kernel type aliases (__u8, __u32, __u64, etc.) - linux/ioctl.h: redirect to MUSL's sys/ioctl.h 2. Updating cfg conditions to include MUSL (target_env = "musl") alongside glibc (target_env = "gnu") 3. Modifying build.rs to prioritize vendored headers via include path This allows building statically-linked TDX attestation binaries using MUSL, which is useful for containerized deployments and systems without glibc. Tested: cargo build --target x86_64-unknown-linux-musl -p tdx-attest
1 parent 745b168 commit a370435

6 files changed

Lines changed: 100 additions & 6 deletions

File tree

tdx-attest-sys/build.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use std::path::PathBuf;
1010
fn main() {
1111
println!("cargo:rerun-if-changed=csrc/tdx_attest.c");
1212
println!("cargo:rerun-if-changed=csrc/qgs_msg_lib.cpp");
13+
println!("cargo:rerun-if-changed=csrc/linux/vm_sockets.h");
1314
let output_path = PathBuf::from(env::var("OUT_DIR").expect("OUT_DIR not set"));
1415
bindgen::Builder::default()
1516
.header("bindings.h")
@@ -21,5 +22,8 @@ fn main() {
2122
cc::Build::new()
2223
.file("csrc/tdx_attest.c")
2324
.file("csrc/qgs_msg_lib.cpp")
25+
// Add csrc to include path first so our vendored linux/vm_sockets.h is found
26+
// before system headers (needed for MUSL builds)
27+
.include("csrc")
2428
.compile("tdx_attest");
2529
}

tdx-attest-sys/csrc/linux/ioctl.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
2+
/*
3+
* Minimal vendored linux/ioctl.h for MUSL compatibility.
4+
*
5+
* This provides the _IO/_IOR/_IOW/_IOWR macros needed by tdx_attest.c
6+
* MUSL provides sys/ioctl.h which already has these, so we just include that.
7+
*/
8+
9+
#ifndef _LINUX_IOCTL_H
10+
#define _LINUX_IOCTL_H
11+
12+
#include <sys/ioctl.h>
13+
14+
#endif /* _LINUX_IOCTL_H */

tdx-attest-sys/csrc/linux/types.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
2+
/*
3+
* Minimal vendored linux/types.h for MUSL compatibility.
4+
*
5+
* Provides kernel-style type aliases (__u8, __u16, __u32, __u64)
6+
* using standard C types from stdint.h.
7+
*/
8+
9+
#ifndef _LINUX_TYPES_H
10+
#define _LINUX_TYPES_H
11+
12+
#include <stdint.h>
13+
14+
typedef uint8_t __u8;
15+
typedef uint16_t __u16;
16+
typedef uint32_t __u32;
17+
typedef uint64_t __u64;
18+
19+
typedef int8_t __s8;
20+
typedef int16_t __s16;
21+
typedef int32_t __s32;
22+
typedef int64_t __s64;
23+
24+
/* Kernel-style sa_family_t if not already defined */
25+
#ifndef __kernel_sa_family_t
26+
typedef unsigned short __kernel_sa_family_t;
27+
#endif
28+
29+
#endif /* _LINUX_TYPES_H */
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
2+
/*
3+
* Minimal vendored version of linux/vm_sockets.h for MUSL compatibility.
4+
*
5+
* This header provides the minimal definitions needed by tdx_attest.c
6+
* without pulling in glibc-specific headers that conflict with MUSL.
7+
*
8+
* Based on: Linux kernel include/uapi/linux/vm_sockets.h
9+
* Original copyright: VMware, Inc.
10+
*/
11+
12+
#ifndef _VM_SOCKETS_H
13+
#define _VM_SOCKETS_H
14+
15+
#include <stdint.h>
16+
#include <sys/socket.h>
17+
18+
/* AF_VSOCK = PF_VSOCK = 40 */
19+
#ifndef AF_VSOCK
20+
#define AF_VSOCK 40
21+
#endif
22+
23+
/* Use this as the destination CID in an address when referring to the host
24+
* (any process other than the hypervisor).
25+
*/
26+
#define VMADDR_CID_HOST 2
27+
28+
/* Address structure for vSockets. The address family should be set to
29+
* AF_VSOCK.
30+
*/
31+
struct sockaddr_vm {
32+
sa_family_t svm_family; /* Address family: AF_VSOCK */
33+
unsigned short svm_reserved1; /* Reserved, must be zero */
34+
unsigned int svm_port; /* Port, in host byte order */
35+
unsigned int svm_cid; /* Context ID (CID) */
36+
uint8_t svm_flags; /* Flags */
37+
unsigned char svm_zero[sizeof(struct sockaddr) -
38+
sizeof(sa_family_t) -
39+
sizeof(unsigned short) -
40+
sizeof(unsigned int) -
41+
sizeof(unsigned int) -
42+
sizeof(uint8_t)];
43+
};
44+
45+
#endif /* _VM_SOCKETS_H */

tdx-attest/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ fs-err.workspace = true
2323
serde_json = { workspace = true, features = ["alloc"] }
2424
sha2.workspace = true
2525

26-
[target.'cfg(all(target_os = "linux", target_arch = "x86_64", target_env = "gnu"))'.dependencies]
26+
# Linux x86_64 with glibc or musl
27+
[target.'cfg(all(target_os = "linux", target_arch = "x86_64"))'.dependencies]
2728
tdx-attest-sys.workspace = true
2829

2930
[dev-dependencies]

tdx-attest/src/lib.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@
22
//
33
// SPDX-License-Identifier: Apache-2.0
44

5-
#[cfg(all(target_os = "linux", target_arch = "x86_64", target_env = "gnu"))]
5+
// Linux x86_64 with glibc or musl
6+
#[cfg(all(target_os = "linux", target_arch = "x86_64"))]
67
pub use linux::*;
7-
#[cfg(all(target_os = "linux", target_arch = "x86_64", target_env = "gnu"))]
8+
#[cfg(all(target_os = "linux", target_arch = "x86_64"))]
89
mod linux;
910

10-
#[cfg(not(all(target_os = "linux", target_arch = "x86_64", target_env = "gnu")))]
11+
// Fallback for non-Linux/non-x86_64 platforms (dummy implementation)
12+
#[cfg(not(all(target_os = "linux", target_arch = "x86_64")))]
1113
pub use dummy::*;
12-
13-
#[cfg(not(all(target_os = "linux", target_arch = "x86_64", target_env = "gnu")))]
14+
#[cfg(not(all(target_os = "linux", target_arch = "x86_64")))]
1415
mod dummy;
1516

1617
pub use cc_eventlog as eventlog;

0 commit comments

Comments
 (0)