Skip to content

Commit 332f3b2

Browse files
committed
refactor traget cageid usage in 3i and rawposix
1 parent f77760a commit 332f3b2

File tree

11 files changed

+165
-490
lines changed

11 files changed

+165
-490
lines changed

src/rawposix/src/fs_calls.rs

Lines changed: 42 additions & 236 deletions
Large diffs are not rendered by default.

src/rawposix/src/net_calls.rs

Lines changed: 18 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -82,15 +82,6 @@ pub extern "C" fn poll_syscall(
8282
return syscall_error(Errno::EFAULT, "poll_syscall", "Invalid Cage ID");
8383
}
8484

85-
// Due to 3i syscall interposition, `cageid` refers to the
86-
// current execution context (possibly a forwarding grate), not
87-
// necessarily the original caller.
88-
//
89-
// For syscalls like `poll`, the operation must be performed on the
90-
// the originating cage. Therefore, we derive the semantic operation
91-
// cage from the argument metadata (`fds_cageid`).
92-
let operation_cageid = fds_cageid;
93-
9485
// Basic bounds checking - validate arguments before conversion - FD_PER_PROCESS_MAX is defined in fdtables constants
9586
if nfds_arg > fdtables::FD_PER_PROCESS_MAX {
9687
return syscall_error(Errno::EINVAL, "poll_syscall", "Too many file descriptors");
@@ -139,7 +130,7 @@ pub extern "C" fn poll_syscall(
139130

140131
// Convert virtual fds to kernel fds by fdkind using fdtables API
141132
let (poll_data_by_fdkind, fdtables_mapping_table) =
142-
fdtables::convert_virtualfds_for_poll(operation_cageid, virtual_fds);
133+
fdtables::convert_virtualfds_for_poll(cageid, virtual_fds);
143134

144135
// Process kernel-backed FDs and handle invalid FDs
145136
let mut all_kernel_pollfds: Vec<libc::pollfd> = Vec::new();
@@ -220,7 +211,7 @@ pub extern "C" fn poll_syscall(
220211
// This implements POSIX signal semantics where poll() should return EINTR
221212
// if interrupted by a signal before any file descriptors become ready or timeout occurs.
222213
// The signal checking happens in the retry loop to ensure we don't block indefinitely
223-
if signal_check_trigger(operation_cageid) {
214+
if signal_check_trigger(cageid) {
224215
return syscall_error(Errno::EINTR, "poll_syscall", "interrupted");
225216
}
226217
}
@@ -307,15 +298,6 @@ pub extern "C" fn select_syscall(
307298
return syscall_error(Errno::EFAULT, "select_syscall", "Invalid Cage ID");
308299
}
309300

310-
// Due to 3i syscall interposition, `cageid` refers to the
311-
// current execution context (possibly a forwarding grate), not
312-
// necessarily the original caller.
313-
//
314-
// For syscalls like `select`, the operation must be performed on the
315-
// the originating cage. Therefore, we derive the semantic operation
316-
// cage from the argument metadata (`nfds_cageid`).
317-
let operation_cageid = nfds_cageid;
318-
319301
// Convert arguments
320302
let nfds = sc_convert_sysarg_to_i32(nfds_arg, nfds_cageid, cageid);
321303

@@ -352,7 +334,7 @@ pub extern "C" fn select_syscall(
352334
// Prepare bitmasks for select using fdtables
353335
let (selectbittables, unparsedtables, mappingtable) =
354336
match fdtables::prepare_bitmasks_for_select(
355-
operation_cageid,
337+
cageid,
356338
nfds as u64,
357339
readfds_ptr.map(|ptr| unsafe { *ptr }),
358340
writefds_ptr.map(|ptr| unsafe { *ptr }),
@@ -476,7 +458,7 @@ pub extern "C" fn select_syscall(
476458
// This implements POSIX signal semantics where select() should return EINTR
477459
// if interrupted by a signal before any file descriptors become ready or timeout occurs.
478460
// The signal checking happens in the retry loop to ensure we don't block indefinitely
479-
if signal_check_trigger(operation_cageid) {
461+
if signal_check_trigger(cageid) {
480462
return syscall_error(Errno::EINTR, "select_syscall", "interrupted");
481463
}
482464
}
@@ -588,15 +570,6 @@ pub extern "C" fn epoll_create_syscall(
588570
// Convert size argument
589571
let size = sc_convert_sysarg_to_i32(size_arg, size_cageid, cageid);
590572

591-
// Due to 3i syscall interposition, `cageid` refers to the
592-
// current execution context (possibly a forwarding grate), not
593-
// necessarily the original caller.
594-
//
595-
// For syscalls like `epoll_create`, the operation must be performed on the
596-
// the originating cage. Therefore, we derive the semantic operation
597-
// cage from the argument metadata (`size_cageid`).
598-
let operation_cageid = size_cageid;
599-
600573
// Create the kernel epoll instance
601574
let kernel_fd = unsafe { libc::epoll_create(size) };
602575

@@ -606,13 +579,8 @@ pub extern "C" fn epoll_create_syscall(
606579
}
607580

608581
// Get the virtual epfd and register to fdtables
609-
let virtual_epfd = fdtables::epoll_create_empty(operation_cageid, false).unwrap();
610-
fdtables::epoll_add_underfd(
611-
operation_cageid,
612-
virtual_epfd,
613-
FDKIND_KERNEL,
614-
kernel_fd as u64,
615-
);
582+
let virtual_epfd = fdtables::epoll_create_empty(cageid, false).unwrap();
583+
fdtables::epoll_add_underfd(cageid, virtual_epfd, FDKIND_KERNEL, kernel_fd as u64);
616584

617585
// Return virtual epfd
618586
virtual_epfd as i32
@@ -671,15 +639,6 @@ pub extern "C" fn epoll_create1_syscall(
671639
// Convert size argument
672640
let flags = sc_convert_sysarg_to_i32(flags_arg, flags_cageid, cageid);
673641

674-
// Due to 3i syscall interposition, `cageid` refers to the
675-
// current execution context (possibly a forwarding grate), not
676-
// necessarily the original caller.
677-
//
678-
// For syscalls like `epoll_create1`, the operation must be performed on the
679-
// the originating cage. Therefore, we derive the semantic operation
680-
// cage from the argument metadata (`flags_cageid`).
681-
let operation_cageid = flags_cageid;
682-
683642
//Validates that the flags argument contains only allowed bits (EPOLL_CLOEXEC),
684643
//returning EINVAL if any unknown flags are detected.
685644
if (flags & !EPOLL_CLOEXEC) != 0 {
@@ -697,7 +656,7 @@ pub extern "C" fn epoll_create1_syscall(
697656
let should_cloexec = (flags & EPOLL_CLOEXEC) != 0;
698657

699658
// Get the virtual epfd and register to fdtables
700-
let virtual_epfd = fdtables::epoll_create_empty(operation_cageid, should_cloexec).unwrap();
659+
let virtual_epfd = fdtables::epoll_create_empty(cageid, should_cloexec).unwrap();
701660
fdtables::epoll_add_underfd(cageid, virtual_epfd, FDKIND_KERNEL, kernel_fd as u64);
702661

703662
// Return virtual epfd
@@ -751,18 +710,9 @@ pub extern "C" fn epoll_ctl_syscall(
751710
return syscall_error(Errno::EFAULT, "epoll_ctl_syscall", "Invalid Cage ID");
752711
}
753712

754-
// Due to 3i syscall interposition, `cageid` refers to the
755-
// current execution context (possibly a forwarding grate), not
756-
// necessarily the original caller.
757-
//
758-
// For syscalls like `epoll_ctl`, the operation must be performed on the
759-
// the originating cage. Therefore, we derive the semantic operation
760-
// cage from the argument metadata (`epfd_cageid`).
761-
let operation_cageid = epfd_cageid;
762-
763713
// Get the underfd of type FDKIND_KERNEL to the vitual fd
764714
// Details see documentation on fdtables/epoll_get_underfd_hashmap.md
765-
let epfd = *fdtables::epoll_get_underfd_hashmap(operation_cageid, epfd_arg)
715+
let epfd = *fdtables::epoll_get_underfd_hashmap(cageid, epfd_arg)
766716
.unwrap()
767717
.get(&FDKIND_KERNEL)
768718
.unwrap();
@@ -774,7 +724,7 @@ pub extern "C" fn epoll_ctl_syscall(
774724

775725
// Translate virtual FDs to kernel FDs. We only need to translate this since this is a
776726
// normal fd, not epfd
777-
let wrappedvfd = fdtables::translate_virtual_fd(operation_cageid, fd_arg);
727+
let wrappedvfd = fdtables::translate_virtual_fd(cageid, fd_arg);
778728
if wrappedvfd.is_err() {
779729
return syscall_error(Errno::EBADF, "epoll_ctl_syscall", "Bad File Descriptor");
780730
}
@@ -909,18 +859,9 @@ pub extern "C" fn epoll_wait_syscall(
909859
return syscall_error(Errno::EFAULT, "epoll_wait_syscall", "Invalid Cage ID");
910860
}
911861

912-
// Due to 3i syscall interposition, `cageid` refers to the
913-
// current execution context (possibly a forwarding grate), not
914-
// necessarily the original caller.
915-
//
916-
// For syscalls like `epoll_wait`, the operation must be performed on the
917-
// the originating cage. Therefore, we derive the semantic operation
918-
// cage from the argument metadata (`epfd_cageid`).
919-
let operation_cageid = epfd_cageid;
920-
921862
// Get the underfd of type FDKIND_KERNEL to the vitual fd
922863
// Details see documentation on fdtables/epoll_get_underfd_hashmap.md
923-
let epfd = *fdtables::epoll_get_underfd_hashmap(operation_cageid, epfd_arg)
864+
let epfd = *fdtables::epoll_get_underfd_hashmap(cageid, epfd_arg)
924865
.unwrap()
925866
.get(&FDKIND_KERNEL)
926867
.unwrap();
@@ -994,7 +935,7 @@ pub extern "C" fn epoll_wait_syscall(
994935
// This implements POSIX signal semantics where epoll() should return EINTR
995936
// if interrupted by a signal before any file descriptors become ready or timeout occurs.
996937
// The signal checking happens in the retry loop to ensure we don't block indefinitely
997-
if signal_check_trigger(operation_cageid) {
938+
if signal_check_trigger(cageid) {
998939
return syscall_error(Errno::EINTR, "epoll", "interrupted");
999940
}
1000941
}
@@ -1065,15 +1006,6 @@ pub extern "C" fn socket_syscall(
10651006
);
10661007
}
10671008

1068-
// Due to 3i syscall interposition, `cageid` refers to the
1069-
// current execution context (possibly a forwarding grate), not
1070-
// necessarily the original caller.
1071-
//
1072-
// For syscalls like `socket`, the operation must be performed on the
1073-
// the originating cage. Therefore, we derive the semantic operation
1074-
// cage from the argument metadata (`domain_cageid`).
1075-
let operation_cageid = domain_cageid;
1076-
10771009
let kernel_fd = unsafe { libc::socket(domain, socktype, protocol) };
10781010

10791011
if kernel_fd < 0 {
@@ -1091,14 +1023,8 @@ pub extern "C" fn socket_syscall(
10911023
// (equivalent to `O_NONBLOCK`). Since our virtual FD maps directly to a
10921024
// host kernel FD (`FDKIND_KERNEL`), we simply defer to the kernel as the
10931025
// source of truth and do not duplicate this flag in `fdtables::optionalinfo`.
1094-
fdtables::get_unused_virtual_fd(
1095-
operation_cageid,
1096-
FDKIND_KERNEL,
1097-
kernel_fd as u64,
1098-
cloexec,
1099-
0,
1100-
)
1101-
.unwrap() as i32
1026+
fdtables::get_unused_virtual_fd(cageid, FDKIND_KERNEL, kernel_fd as u64, cloexec, 0).unwrap()
1027+
as i32
11021028
}
11031029

11041030
/// Reference to Linux: https://man7.org/linux/man-pages/man2/connect.2.html
@@ -1322,15 +1248,6 @@ pub extern "C" fn accept_syscall(
13221248
);
13231249
}
13241250

1325-
// Due to 3i syscall interposition, `cageid` refers to the
1326-
// current execution context (possibly a forwarding grate), not
1327-
// necessarily the original caller.
1328-
//
1329-
// For syscalls like `accept`, the operation must be performed on the
1330-
// the originating cage. Therefore, we derive the semantic operation
1331-
// cage from the argument metadata (`fd_cageid`).
1332-
let operation_cageid = fd_cageid;
1333-
13341251
let (finalsockaddr, mut addrlen) = convert_host_sockaddr(addr, addr_cageid, cageid);
13351252

13361253
let ret_kernelfd = unsafe { libc::accept(fd, finalsockaddr, &mut addrlen as *mut u32) };
@@ -1341,14 +1258,9 @@ pub extern "C" fn accept_syscall(
13411258
}
13421259

13431260
// We need to register this new kernel fd in fdtables
1344-
let ret_virtualfd = fdtables::get_unused_virtual_fd(
1345-
operation_cageid,
1346-
FDKIND_KERNEL,
1347-
ret_kernelfd as u64,
1348-
false,
1349-
0,
1350-
)
1351-
.unwrap();
1261+
let ret_virtualfd =
1262+
fdtables::get_unused_virtual_fd(cageid, FDKIND_KERNEL, ret_kernelfd as u64, false, 0)
1263+
.unwrap();
13521264

13531265
ret_virtualfd as i32
13541266
}
@@ -2041,15 +1953,6 @@ pub extern "C" fn socketpair_syscall(
20411953
);
20421954
}
20431955

2044-
// Due to 3i syscall interposition, `cageid` refers to the
2045-
// current execution context (possibly a forwarding grate), not
2046-
// necessarily the original caller.
2047-
//
2048-
// For syscalls like `socketpair`, the operation must be performed on the
2049-
// the originating cage. Therefore, we derive the semantic operation
2050-
// cage from the argument metadata (`domain_cageid`).
2051-
let operation_cageid = domain_cageid;
2052-
20531956
let mut kernel_socket_vector: [i32; 2] = [0, 0];
20541957

20551958
let ret = unsafe { libc::socketpair(domain, typ, protocol, kernel_socket_vector.as_mut_ptr()) };
@@ -2072,11 +1975,9 @@ pub extern "C" fn socketpair_syscall(
20721975
// host kernel FD (`FDKIND_KERNEL`), we simply defer to the kernel as the
20731976
// source of truth and do not duplicate this flag in `fdtables::optionalinfo`.
20741977
let vsv_1 =
2075-
fdtables::get_unused_virtual_fd(operation_cageid, FDKIND_KERNEL, ksv_1 as u64, cloexec, 0)
2076-
.unwrap();
1978+
fdtables::get_unused_virtual_fd(cageid, FDKIND_KERNEL, ksv_1 as u64, cloexec, 0).unwrap();
20771979
let vsv_2 =
2078-
fdtables::get_unused_virtual_fd(operation_cageid, FDKIND_KERNEL, ksv_2 as u64, cloexec, 0)
2079-
.unwrap();
1980+
fdtables::get_unused_virtual_fd(cageid, FDKIND_KERNEL, ksv_2 as u64, cloexec, 0).unwrap();
20801981

20811982
// Update virtual socketpair struct
20821983
virtual_socket_vector.sock1 = vsv_1 as i32;

src/rawposix/src/sys_calls.rs

Lines changed: 6 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -461,17 +461,8 @@ pub extern "C" fn waitpid_syscall(
461461
);
462462
}
463463

464-
// Due to 3i syscall interposition, `cageid` refers to the
465-
// current execution context (possibly a forwarding grate), not
466-
// necessarily the original caller.
467-
//
468-
// For syscalls like `waitpid`, the operation must be performed on the
469-
// the originating cage. Therefore, we derive the semantic operation
470-
// cage from the argument metadata (`status_cageid`).
471-
let operation_cageid = status_cageid;
472-
473464
// get the cage instance
474-
let cage = get_cage(operation_cageid).unwrap();
465+
let cage = get_cage(cageid).unwrap();
475466

476467
let mut zombies = cage.zombies.write();
477468
let child_num = cage.child_num.load(Relaxed);
@@ -643,16 +634,7 @@ pub extern "C" fn getpid_syscall(
643634
);
644635
}
645636

646-
// Due to 3i syscall interposition, `cageid` refers to the
647-
// current execution context (possibly a forwarding grate), not
648-
// necessarily the original caller.
649-
//
650-
// For syscalls like `getpid`, the operation must be performed on the
651-
// the originating cage. Therefore, we derive the semantic operation
652-
// cage from the argument metadata (`arg1_cageid`).
653-
let operation_cageid = arg1_cageid;
654-
655-
let cage = get_cage(operation_cageid).unwrap();
637+
let cage = get_cage(cageid).unwrap();
656638

657639
return cage.cageid as i32;
658640
}
@@ -690,16 +672,7 @@ pub extern "C" fn getppid_syscall(
690672
return syscall_error(Errno::EFAULT, "getppid", "invalid Cage ID");
691673
}
692674

693-
// Due to 3i syscall interposition, `cageid` refers to the
694-
// current execution context (possibly a forwarding grate), not
695-
// necessarily the original caller.
696-
//
697-
// For syscalls like `getppid`, the operation must be performed on the
698-
// the originating cage. Therefore, we derive the semantic operation
699-
// cage from the argument metadata (`arg1_cageid`).
700-
let operation_cageid = arg1_cageid;
701-
702-
let cage = get_cage(operation_cageid).unwrap();
675+
let cage = get_cage(cageid).unwrap();
703676

704677
return cage.parent as i32;
705678
}
@@ -904,17 +877,8 @@ pub extern "C" fn sigaction_syscall(
904877
);
905878
}
906879

907-
// Due to 3i syscall interposition, `cageid` refers to the
908-
// current execution context (possibly a forwarding grate), not
909-
// necessarily the original caller.
910-
//
911-
// For syscalls like `sigaction`, the operation must be performed on the
912-
// the originating cage. Therefore, we derive the semantic operation
913-
// cage from the argument metadata (`sig_arg_cageid`).
914-
let operation_cageid = sig_arg_cageid;
915-
916880
// Retrieve the cage.
917-
let cage = match get_cage(operation_cageid) {
881+
let cage = match get_cage(cageid) {
918882
Some(c) => c,
919883
None => return syscall_error(Errno::ECHILD, "sigaction", "Cage not found"),
920884
};
@@ -1087,16 +1051,7 @@ pub extern "C" fn sigprocmask_syscall(
10871051
);
10881052
}
10891053

1090-
// Due to 3i syscall interposition, `cageid` refers to the
1091-
// current execution context (possibly a forwarding grate), not
1092-
// necessarily the original caller.
1093-
//
1094-
// For syscalls like `sigprocmask`, the operation must be performed on the
1095-
// the originating cage. Therefore, we derive the semantic operation
1096-
// cage from the argument metadata (`how_cageid`).
1097-
let operation_cageid = how_cageid;
1098-
1099-
let cage = get_cage(operation_cageid).unwrap();
1054+
let cage = get_cage(cageid).unwrap();
11001055

11011056
let mut res = 0;
11021057

@@ -1246,17 +1201,8 @@ pub extern "C" fn setitimer_syscall(
12461201
);
12471202
}
12481203

1249-
// Due to 3i syscall interposition, `cageid` refers to the
1250-
// current execution context (possibly a forwarding grate), not
1251-
// necessarily the original caller.
1252-
//
1253-
// For syscalls like `setitimer`, the operation must be performed on the
1254-
// the originating cage. Therefore, we derive the semantic operation
1255-
// cage from the argument metadata (`which_arg_cageid`).
1256-
let operation_cageid = which_arg_cageid;
1257-
12581204
// get the cage instance
1259-
let cage = get_cage(operation_cageid).unwrap();
1205+
let cage = get_cage(cageid).unwrap();
12601206

12611207
match which {
12621208
ITIMER_REAL => {

0 commit comments

Comments
 (0)