interplane.Get Omega
Read in your language

CUDApp

CUDA++: AIEN’s GPU machine-code toolchain, runtime and driver client for NVIDIA GPUs without the CUDA toolkit. Hardware coverage and driver replacement limits are explicit.

Brought by drakestapleton

branch main Branches Tags History

CUDApp / nvrm / native_ioctl_fixture.h

181 lines · 9861 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */
2/* Test oracle only: trap ioctls to one deliberately unopened fd in a child.
3 * Ordinary Linux syscall execution and Omega's request construction are real;
4 * the SIGSYS handler stands in for the RM/UVM kernel and checks every byte. */
5#include <linux/filter.h>
6#include <linux/seccomp.h>
7#include <stddef.h>
8#include <sys/prctl.h>
9#include <sys/syscall.h>
10#include <sys/wait.h>
11#include <ucontext.h>
12static unsigned char fixture_expected[9264];
13static size_t fixture_size,fixture_status;
14static unsigned long fixture_request;
15static volatile sig_atomic_t fixture_seen,fixture_bad;
16static int fixture_mutant;
17static unsigned char fixture_params[16];
18static void fixture_sigsys(int signal,siginfo_t *info,void *opaque) {
19 (void)signal;
20 ucontext_t *uc=opaque;
21 unsigned long request; unsigned char *bytes;
22#if defined(__aarch64__)
23 request=uc->uc_mcontext.regs[1];bytes=(void *)(uintptr_t)uc->uc_mcontext.regs[2];
24 uc->uc_mcontext.regs[0]=0;
25#elif defined(__x86_64__)
26 request=uc->uc_mcontext.gregs[REG_RSI];bytes=(void *)(uintptr_t)uc->uc_mcontext.gregs[REG_RDX];
27 uc->uc_mcontext.gregs[REG_RAX]=0;
28#else
29#error "NVRM native syscall fixture requires Linux AArch64 or x86-64"
30#endif
31 fixture_seen++;
32 if(info->si_syscall!=SYS_ioctl || request!=(fixture_request^(unsigned)fixture_mutant))fixture_bad=1;
33 for(size_t i=0;i<fixture_size;i++)if(bytes[i]!=fixture_expected[i])fixture_bad=1;
34 if(fixture_status<fixture_size) {
35 uint32_t reply=0x0badc0de;memcpy(bytes+fixture_status,&reply,sizeof reply);
36 }
37 if(request==NV_IOWR(NV_ESC_RM_ALLOC,sizeof(NVOS21_PARAMETERS)) ||
38 request==NV_IOWR(NV_ESC_RM_CONTROL,sizeof(NVOS54_PARAMETERS))) {
39 uintptr_t pointer=0;memcpy(&pointer,bytes+16,sizeof pointer);
40 if(pointer!=(uintptr_t)fixture_params)fixture_bad=1;
41 else ((unsigned char *)pointer)[0]=0x42;
42 }
43}
44static void fixture_prepare(unsigned long request,const void *bytes,size_t size,size_t status) {
45 memcpy(fixture_expected,bytes,size);fixture_request=request;fixture_size=size;fixture_status=status;fixture_seen=0;
46}
47#define FIXTURE_RM(type,nr,status_field) do { \
48 type p;memset(&p,0,sizeof p); \
49 fixture_prepare(NV_IOWR(nr,sizeof p),&p,sizeof p,offsetof(type,status_field)); \
50 assert(nvrm_rm_ioctl(12345,nr,&p,sizeof p)==0); \
51 assert(fixture_seen==1 && p.status_field==0x0badc0de); \
52} while(0)
53#define FIXTURE_UVM(type,command) do { \
54 type p;memset(&p,0,sizeof p); \
55 fixture_prepare(command,&p,sizeof p,offsetof(type,rmStatus)); \
56 assert(nvrm_uvm_ioctl(12345,command,&p)==0); \
57 assert(fixture_seen==1 && p.rmStatus==0x0badc0de); \
58} while(0)
59static int fixture_child(void) {
60 struct sigaction action={0};action.sa_sigaction=fixture_sigsys;action.sa_flags=SA_SIGINFO;
61 sigemptyset(&action.sa_mask);assert(sigaction(SIGSYS,&action,NULL)==0);
62 struct sock_filter filters[]={
63 BPF_STMT(BPF_LD|BPF_W|BPF_ABS,offsetof(struct seccomp_data,nr)),
64 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K,SYS_ioctl,0,3),
65 BPF_STMT(BPF_LD|BPF_W|BPF_ABS,offsetof(struct seccomp_data,args[0])),
66 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K,12345,0,1),
67 BPF_STMT(BPF_RET|BPF_K,SECCOMP_RET_TRAP),
68 BPF_STMT(BPF_RET|BPF_K,SECCOMP_RET_ALLOW)
69 };
70 struct sock_fprog program={.len=sizeof filters/sizeof filters[0],.filter=filters};
71 assert(prctl(PR_SET_NO_NEW_PRIVS,1,0,0,0)==0);
72 assert(prctl(PR_SET_SECCOMP,SECCOMP_MODE_FILTER,&program)==0);
73 FIXTURE_RM(NVOS00_PARAMETERS,NV_ESC_RM_FREE,status);
74 FIXTURE_RM(nv_ioctl_nvos33_parameters_with_fd,NV_ESC_RM_MAP_MEMORY,params.status);
75 FIXTURE_RM(NVOS46_PARAMETERS,NV_ESC_RM_MAP_MEMORY_DMA,status);
76 FIXTURE_RM(NVOS47_PARAMETERS,NV_ESC_RM_UNMAP_MEMORY_DMA,status);
77 FIXTURE_RM(NVOS32_PARAMETERS,NV_ESC_RM_VID_HEAP_CONTROL,status);
78 nv_ioctl_register_fd_t registration={.ctl_fd=71};
79 fixture_prepare(NV_IOWR(NV_ESC_REGISTER_FD,sizeof registration),&registration,sizeof registration,SIZE_MAX);
80 assert(nvrm_rm_ioctl(12345,NV_ESC_REGISTER_FD,&registration,sizeof registration)==0 && fixture_seen==1);
81 nv_ioctl_card_info_t cards[64];memset(cards,0,sizeof cards);
82 fixture_prepare(NV_IOWR(NV_ESC_CARD_INFO,sizeof cards),cards,sizeof cards,SIZE_MAX);
83 assert(nvrm_rm_ioctl(12345,NV_ESC_CARD_INFO,cards,sizeof cards)==0 && fixture_seen==1);
84 NVOS21_PARAMETERS allocation={.hRoot=11,.hObjectParent=22,.hObjectNew=33,.hClass=44,
85 .pAllocParms=fixture_params,.paramsSize=sizeof fixture_params};
86 fixture_prepare(NV_IOWR(NV_ESC_RM_ALLOC,sizeof allocation),&allocation,sizeof allocation,offsetof(NVOS21_PARAMETERS,status));
87 assert(nvrm_rm_ioctl(12345,NV_ESC_RM_ALLOC,&allocation,sizeof allocation)==0 && fixture_seen==1);
88 assert(allocation.status==0x0badc0de && fixture_params[0]==0x42 && allocation.pAllocParms==0);
89 NVOS54_PARAMETERS control={.hClient=11,.hObject=22,.cmd=33,.flags=44,
90 .params=fixture_params,.paramsSize=sizeof fixture_params};
91 fixture_params[0]=0;
92 fixture_prepare(NV_IOWR(NV_ESC_RM_CONTROL,sizeof control),&control,sizeof control,offsetof(NVOS54_PARAMETERS,status));
93 assert(nvrm_rm_ioctl(12345,NV_ESC_RM_CONTROL,&control,sizeof control)==0 && fixture_seen==1);
94 assert(control.status==0x0badc0de && fixture_params[0]==0x42 && control.params==0);
95 FIXTURE_UVM(UVM_INITIALIZE_PARAMS,UVM_INITIALIZE);
96 FIXTURE_UVM(UVM_MM_INITIALIZE_PARAMS,UVM_MM_INITIALIZE);
97 FIXTURE_UVM(UVM_REGISTER_GPU_PARAMS,UVM_REGISTER_GPU);
98 FIXTURE_UVM(UVM_UNREGISTER_GPU_PARAMS,UVM_UNREGISTER_GPU);
99 FIXTURE_UVM(UVM_REGISTER_GPU_VASPACE_PARAMS,UVM_REGISTER_GPU_VASPACE);
100 FIXTURE_UVM(UVM_UNREGISTER_GPU_VASPACE_PARAMS,UVM_UNREGISTER_GPU_VASPACE);
101 FIXTURE_UVM(UVM_REGISTER_CHANNEL_PARAMS,UVM_REGISTER_CHANNEL);
102 FIXTURE_UVM(UVM_UNREGISTER_CHANNEL_PARAMS,UVM_UNREGISTER_CHANNEL);
103 FIXTURE_UVM(UVM_MAP_EXTERNAL_ALLOCATION_PARAMS,UVM_MAP_EXTERNAL_ALLOCATION);
104 FIXTURE_UVM(UVM_FREE_PARAMS,UVM_FREE);
105 FIXTURE_UVM(UVM_CREATE_EXTERNAL_RANGE_PARAMS,UVM_CREATE_EXTERNAL_RANGE);
106 return fixture_bad ? 1 : 0;
107}
108static void fixture_run(void) {
109 for(int mutant=0;mutant<=1;mutant++) {
110 pid_t child=fork();assert(child>=0);
111 if(!child){fixture_mutant=mutant;_exit(fixture_child());}
112 int status=0;assert(waitpid(child,&status,0)==child && WIFEXITED(status));
113 assert(WEXITSTATUS(status)==mutant);
114 }
115 puts("NVRM NATIVE IOCTL PASS (all RM/UVM request numbers and bytes, fake nonzero replies, parameter copyback, syscall request mutant caught; HOST_ONLY)");
116}
117#undef FIXTURE_RM
118#undef FIXTURE_UVM
119
120/* One denied cleanup followed by a real retry. The test-only third syscall
121 * argument lets the SIGSYS handler forward munmap without recursively trapping;
122 * Linux munmap ignores it. No production syscall or bridge gate is changed. */
123static volatile sig_atomic_t fixture_cleanup_seen;
124static void fixture_cleanup_sigsys(int signal,siginfo_t *info,void *opaque) {
125 (void)signal;
126 ucontext_t *uc=opaque;
127 uintptr_t address;size_t size;long result;
128#if defined(__aarch64__)
129 address=uc->uc_mcontext.regs[0];size=uc->uc_mcontext.regs[1];
130#elif defined(__x86_64__)
131 address=uc->uc_mcontext.gregs[REG_RDI];size=uc->uc_mcontext.gregs[REG_RSI];
132#endif
133 if(info->si_syscall!=SYS_munmap)_exit(80);
134 fixture_cleanup_seen++;
135 int saved_errno=errno;
136 result=fixture_cleanup_seen==1?-EPERM:syscall(SYS_munmap,address,size,0x43554441);
137 errno=saved_errno;
138#if defined(__aarch64__)
139 uc->uc_mcontext.regs[0]=(uint64_t)result;
140#elif defined(__x86_64__)
141 uc->uc_mcontext.gregs[REG_RAX]=result;
142#endif
143}
144static void fixture_cleanup_child(void) {
145 Nvrm rm={0};
146 void *address=(void *)(uintptr_t)UINT64_C(0x7000000000);
147 assert(nvrm_cpu_reserve(&rm,(uintptr_t)address,4096)==address);
148 uint64_t owner=rm.omega_pending_mapping;
149 int fd=memfd_create("cudapp-nvrm-cleanup-fixture",MFD_CLOEXEC);
150 assert(fd>=0 && ftruncate(fd,4096)==0);
151 struct sigaction action={0};action.sa_sigaction=fixture_cleanup_sigsys;action.sa_flags=SA_SIGINFO;
152 sigemptyset(&action.sa_mask);assert(sigaction(SIGSYS,&action,NULL)==0);
153 struct sock_filter filters[]={
154 BPF_STMT(BPF_LD|BPF_W|BPF_ABS,offsetof(struct seccomp_data,nr)),
155 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K,SYS_munmap,0,3),
156 BPF_STMT(BPF_LD|BPF_W|BPF_ABS,offsetof(struct seccomp_data,args[2])),
157 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K,0x43554441,1,0),
158 BPF_STMT(BPF_RET|BPF_K,SECCOMP_RET_TRAP),
159 BPF_STMT(BPF_RET|BPF_K,SECCOMP_RET_ALLOW)
160 };
161 struct sock_fprog program={.len=sizeof filters/sizeof filters[0],.filter=filters};
162 assert(prctl(PR_SET_NO_NEW_PRIVS,1,0,0,0)==0);
163 assert(prctl(PR_SET_SECCOMP,SECCOMP_MODE_FILTER,&program)==0);
164 assert(nvrm_cpu_map(&rm,-1,4096,address)==MAP_FAILED && errno==EPROTO);
165 assert(fixture_cleanup_seen==1 && rm.omega_pending_mapping==owner);
166 assert(rm.omega_pending_address==(uintptr_t)address && rm.omega_pending_size==4096);
167 uint64_t result=0x12345678;
168 assert(omega2_ctx_nvrm_bind(rm.omega_context,&result,owner,(uint64_t)fd)!=0);
169 assert(result==0x12345678 && fixture_cleanup_seen==1);
170 assert(omega_native_context_destroy(rm.omega_context)==36);
171 assert(nvrm_cpu_drop(&rm,address,4096)==0 && !rm.omega_pending_mapping);
172 assert(fixture_cleanup_seen==2);
173 assert(nvrm_mapping_finish(&rm)==0 && !rm.omega_context);
174 assert(close(fd)==0);
175}
176static void fixture_cleanup_run(void) {
177 pid_t child=fork();assert(child>=0);
178 if(!child){fixture_cleanup_child();_exit(0);}
179 int status=0;assert(waitpid(child,&status,0)==child && WIFEXITED(status) && WEXITSTATUS(status)==0);
180 puts("NVRM CLEANUP FAILURE PASS (failed bind plus denied munmap retains quarantined owner, refuses rebind/destroy, retry drops; HOST_ONLY)");
181}