From 52dddd460b0778f862c062c82d8d2819f740c268 Mon Sep 17 00:00:00 2001 From: Paul Floyd Date: Sat, 23 Aug 2025 14:19:33 +0200 Subject: [PATCH] FreeBSD syscalls: fd handling (part 2) amd64 and x86 files --- coregrind/m_syswrap/syswrap-amd64-freebsd.c | 52 ++++++++++------- coregrind/m_syswrap/syswrap-freebsd.c | 2 + coregrind/m_syswrap/syswrap-x86-freebsd.c | 64 ++++++++++++--------- 3 files changed, 68 insertions(+), 50 deletions(-) diff --git a/coregrind/m_syswrap/syswrap-amd64-freebsd.c b/coregrind/m_syswrap/syswrap-amd64-freebsd.c index 21f22b5a8..71d1bc8b9 100644 --- a/coregrind/m_syswrap/syswrap-amd64-freebsd.c +++ b/coregrind/m_syswrap/syswrap-amd64-freebsd.c @@ -268,17 +268,16 @@ PRE(sys_preadv) int, iovcnt, vki_off_t, offset); if (!ML_(fd_allowed)(ARG1, "preadv", tid, False)) { SET_STATUS_Failure( VKI_EBADF ); - } else { - if ((Int)ARG3 > 0) { - PRE_MEM_READ( "preadv(iov)", ARG2, ARG3 * sizeof(struct vki_iovec) ); - } + } + if ((Int)ARG3 > 0) { + PRE_MEM_READ( "preadv(iov)", ARG2, ARG3 * sizeof(struct vki_iovec) ); + } - if (ML_(safe_to_deref)((struct vki_iovec *)ARG2, ARG3 * sizeof(struct vki_iovec))) { - vec = (struct vki_iovec *)(Addr)ARG2; - for (i = 0; i < (Int)ARG3; i++) { - VG_(sprintf)(buf, "preadv(iov[%d])", i); - PRE_MEM_WRITE(buf, (Addr)vec[i].iov_base, vec[i].iov_len); - } + if (ML_(safe_to_deref)((struct vki_iovec *)ARG2, ARG3 * sizeof(struct vki_iovec))) { + vec = (struct vki_iovec *)(Addr)ARG2; + for (i = 0; i < (Int)ARG3; i++) { + VG_(sprintf)(buf, "preadv(iov[%d])", i); + PRE_MEM_WRITE(buf, (Addr)vec[i].iov_base, vec[i].iov_len); } } } @@ -323,16 +322,15 @@ PRE(sys_pwritev) vki_off_t, offset); if (!ML_(fd_allowed)(ARG1, "pwritev", tid, False)) { SET_STATUS_Failure( VKI_EBADF ); - } else { - if ((Int)ARG3 >= 0) { - PRE_MEM_READ( "pwritev(vector)", ARG2, ARG3 * sizeof(struct vki_iovec) ); - } - if (ML_(safe_to_deref)((struct vki_iovec *)ARG2, ARG3 * sizeof(struct vki_iovec))) { - vec = (struct vki_iovec *)(Addr)ARG2; - for (i = 0; i < (Int)ARG3; i++) { - VG_(sprintf)(buf, "pwritev(iov[%d])", i); - PRE_MEM_READ(buf, (Addr)vec[i].iov_base, vec[i].iov_len ); - } + } + if ((Int)ARG3 >= 0) { + PRE_MEM_READ( "pwritev(vector)", ARG2, ARG3 * sizeof(struct vki_iovec) ); + } + if (ML_(safe_to_deref)((struct vki_iovec *)ARG2, ARG3 * sizeof(struct vki_iovec))) { + vec = (struct vki_iovec *)(Addr)ARG2; + for (i = 0; i < (Int)ARG3; i++) { + VG_(sprintf)(buf, "pwritev(iov[%d])", i); + PRE_MEM_READ(buf, (Addr)vec[i].iov_base, vec[i].iov_len ); } } } @@ -349,7 +347,8 @@ PRE(sys_sendfile) PRE_REG_READ7(int, "sendfile", int, fd, int, s, vki_off_t, offset, size_t, nbytes, void *, hdtr, vki_off_t *, sbytes, int, flags); - + if (!ML_(fd_allowed)(ARG1, "sendfile", tid, False)) + SET_STATUS_Failure( VKI_EBADF ); if (ARG5 != 0) { PRE_MEM_READ("sendfile(hdtr)", ARG5, sizeof(struct vki_sf_hdtr)); } @@ -761,6 +760,8 @@ PRE(sys_lseek) PRE_REG_READ3(long, "lseek", unsigned int, fd, unsigned long, offset, unsigned int, whence); + if (!ML_(fd_allowed)(ARG1, "lseek", tid, False)) + SET_STATUS_Failure(VKI_EBADF); } // SYS_truncate 479 @@ -782,6 +783,8 @@ PRE(sys_ftruncate) PRINT("sys_ftruncate ( %" FMT_REGWORD "u, %" FMT_REGWORD "u )", ARG1,ARG2); PRE_REG_READ2(long, "ftruncate", unsigned int, fd, unsigned long, length); + if (!ML_(fd_allowed)(ARG1, "ftruncate", tid, False)) + SET_STATUS_Failure(VKI_EBADF); } // SYS_cpuset_setid 485 @@ -855,6 +858,8 @@ PRE(sys_posix_fallocate) PRE_REG_READ3(long, "posix_fallocate", int, fd, vki_off_t, offset, vki_off_t, len); + if (!ML_(fd_allowed)(ARG1, "posix_fallocate", tid, False)) + SET_STATUS_Failure(VKI_EBADF); } // SYS_posix_fadvise 531 @@ -867,7 +872,8 @@ PRE(sys_posix_fadvise) int, fd, off_t, offset, off_t, len, int, advice); - // @todo PJF advice can be 0 to 5 inclusive + if (!ML_(fd_allowed)(ARG1, "posix_fadvise", tid, False)) + SET_STATUS_Failure(VKI_EBADF); } // SYS_wait6 532 @@ -981,6 +987,8 @@ PRE(sys_mknodat) PRE_REG_READ4(long, "mknodat", int, fd, const char *, path, vki_mode_t, mode, vki_dev_t, dev); PRE_MEM_RASCIIZ( "mknodat(pathname)", ARG2 ); + if (!ML_(fd_allowed)(ARG1, "mknodat", tid, False)) + SET_STATUS_Failure(VKI_EBADF); } // SYS_cpuset_getdomain 561 diff --git a/coregrind/m_syswrap/syswrap-freebsd.c b/coregrind/m_syswrap/syswrap-freebsd.c index 5ae7b4856..d92a1a46c 100644 --- a/coregrind/m_syswrap/syswrap-freebsd.c +++ b/coregrind/m_syswrap/syswrap-freebsd.c @@ -3847,6 +3847,8 @@ PRE(sys_freebsd11_fstatfs) PRINT("sys_fstatfs ( %" FMT_REGWORD "u, %#" FMT_REGWORD "x )",ARG1,ARG2); PRE_REG_READ2(int, "fstatfs", unsigned int, fd, struct statfs *, buf); + if (!ML_(fd_allowed)(ARG1, "freebsd11_fstatfs", tid, False)) + SET_STATUS_Failure(VKI_EBADF); PRE_MEM_WRITE( "fstatfs(buf)", ARG2, sizeof(struct vki_freebsd11_statfs) ); } diff --git a/coregrind/m_syswrap/syswrap-x86-freebsd.c b/coregrind/m_syswrap/syswrap-x86-freebsd.c index 0ad3dda26..421d7c575 100644 --- a/coregrind/m_syswrap/syswrap-x86-freebsd.c +++ b/coregrind/m_syswrap/syswrap-x86-freebsd.c @@ -679,18 +679,16 @@ PRE(sys_preadv) int, fd, const struct iovec *, iov, int, iovcnt, vki_uint32_t, MERGE64_FIRST(offset), vki_uint32_t, MERGE64_SECOND(offset)); - if (!ML_(fd_allowed)(ARG1, "preadv", tid, False)) { - SET_STATUS_Failure( VKI_EBADF ); - } else { - if ((Int)ARG3 > 0) - PRE_MEM_READ( "preadv(iov)", ARG2, ARG3 * sizeof(struct vki_iovec) ); - - if (ML_(safe_to_deref)((struct vki_iovec *)ARG2, ARG3 * sizeof(struct vki_iovec))) { - vec = (struct vki_iovec *)(Addr)ARG2; - for (i = 0; i < (Int)ARG3; i++) - PRE_MEM_WRITE( "preadv(iov[...])", - (Addr)vec[i].iov_base, vec[i].iov_len ); - } + if (!ML_(fd_allowed)(ARG1, "preadv", tid, False)) + SET_STATUS_Failure(VKI_EBADF); + if ((Int)ARG3 > 0) + PRE_MEM_READ( "preadv(iov)", ARG2, ARG3 * sizeof(struct vki_iovec) ); + + if (ML_(safe_to_deref)((struct vki_iovec *)ARG2, ARG3 * sizeof(struct vki_iovec))) { + vec = (struct vki_iovec *)(Addr)ARG2; + for (i = 0; i < (Int)ARG3; i++) + PRE_MEM_WRITE( "preadv(iov[...])", + (Addr)vec[i].iov_base, vec[i].iov_len ); } } @@ -728,17 +726,15 @@ PRE(sys_pwritev) int, iovcnt, vki_uint32_t, MERGE64_FIRST(offset), vki_uint32_t, MERGE64_SECOND(offset)); - if (!ML_(fd_allowed)(ARG1, "pwritev", tid, False)) { + if (!ML_(fd_allowed)(ARG1, "pwritev", tid, False)) SET_STATUS_Failure( VKI_EBADF ); - } else { - if ((Int)ARG3 >= 0) - PRE_MEM_READ( "pwritev(vector)", ARG2, ARG3 * sizeof(struct vki_iovec) ); - if (ML_(safe_to_deref)((struct vki_iovec *)ARG2, ARG3 * sizeof(struct vki_iovec))) { - vec = (struct vki_iovec *)(Addr)ARG2; - for (i = 0; i < (Int)ARG3; i++) - PRE_MEM_READ( "pwritev(iov[...])", - (Addr)vec[i].iov_base, vec[i].iov_len ); - } + if ((Int)ARG3 >= 0) + PRE_MEM_READ( "pwritev(vector)", ARG2, ARG3 * sizeof(struct vki_iovec) ); + if (ML_(safe_to_deref)((struct vki_iovec *)ARG2, ARG3 * sizeof(struct vki_iovec))) { + vec = (struct vki_iovec *)(Addr)ARG2; + for (i = 0; i < (Int)ARG3; i++) + PRE_MEM_READ( "pwritev(iov[...])", + (Addr)vec[i].iov_base, vec[i].iov_len ); } } @@ -754,7 +750,8 @@ PRE(sys_sendfile) int, fd, int, s, unsigned int, offset_low, unsigned int, offset_high, size_t, nbytes, void *, hdtr, vki_off_t *, sbytes, int, flags); - + if (!ML_(fd_allowed)(ARG1, "sendfile", tid, False)) + SET_STATUS_Failure( VKI_EBADF ); if (ARG6 != 0) PRE_MEM_READ("sendfile(hdtr)", ARG6, sizeof(struct vki_sf_hdtr)); @@ -1091,8 +1088,7 @@ PRE(sys_pread) if (!ML_(fd_allowed)(ARG1, "pread", tid, False)) SET_STATUS_Failure( VKI_EBADF ); - else - PRE_MEM_WRITE( "pread(buf)", ARG2, ARG3 ); + PRE_MEM_WRITE( "pread(buf)", ARG2, ARG3 ); } POST(sys_pread) @@ -1120,8 +1116,7 @@ PRE(sys_pwrite) ok = True; if (!ok) SET_STATUS_Failure( VKI_EBADF ); - else - PRE_MEM_READ( "pwrite(buf)", ARG2, ARG3 ); + PRE_MEM_READ( "pwrite(buf)", ARG2, ARG3 ); } // SYS_mmap 477 @@ -1147,10 +1142,12 @@ PRE(sys_lseek) { PRINT("sys_lseek ( %" FMT_REGWORD "d, %llu, %" FMT_REGWORD "d )", SARG1,MERGE64(ARG2,ARG3),SARG4); PRE_REG_READ4(long, "lseek", - unsigned int, fd, + int, fildes, vki_uint32_t, MERGE64_FIRST(offset), vki_uint32_t, MERGE64_SECOND(offset), unsigned int, whence); + if (!ML_(fd_allowed)(ARG1, "lseek", tid, False)) + SET_STATUS_Failure(VKI_EBADF); } // SYS_truncate 479 @@ -1175,6 +1172,8 @@ PRE(sys_ftruncate) PRE_REG_READ3(int, "ftruncate", int, fd, vki_uint32_t, MERGE64_FIRST(length), vki_uint32_t, MERGE64_SECOND(length)); + if (!ML_(fd_allowed)(ARG1, "ftruncate", tid, False)) + SET_STATUS_Failure(VKI_EBADF); } // SYS_cpuset_setid 485 @@ -1258,6 +1257,8 @@ PRE(sys_posix_fallocate) vki_uint32_t, MERGE64_SECOND(offset), vki_uint32_t, MERGE64_FIRST(len), vki_uint32_t, MERGE64_SECOND(len)); + if (!ML_(fd_allowed)(ARG1, "posix_fallocate", tid, False)) + SET_STATUS_Failure(VKI_EBADF); } // SYS_posix_fadvise 531 @@ -1272,6 +1273,8 @@ PRE(sys_posix_fadvise) vki_uint32_t, MERGE64_FIRST(len), vki_uint32_t, MERGE64_SECOND(len), int, advice); + if (!ML_(fd_allowed)(ARG1, "posix_fadvise", tid, False)) + SET_STATUS_Failure(VKI_EBADF); } // SYS_wait6 532 @@ -1385,10 +1388,15 @@ POST(sys_procctl) // int mknodat(int fd, const char *path, mode_t mode, dev_t dev); PRE(sys_mknodat) { + Int arg_1 = (Int)ARG1; + const HChar *path = (const HChar*)ARG2; PRINT("sys_mknodat ( %" FMT_REGWORD "u, %#" FMT_REGWORD "x(%s), 0x%" FMT_REGWORD "x, 0x%" FMT_REGWORD "x )", ARG1,ARG2,(char*)ARG2,ARG3,ARG4 ); PRE_REG_READ5(long, "mknodat", int, fd, const char *, path, vki_mode_t, mode, vki_uint32_t, MERGE64_FIRST(dev), vki_uint32_t, MERGE64_SECOND(idev)) PRE_MEM_RASCIIZ( "mknodat(pathname)", ARG2 ); + if ((ML_(safe_to_deref)(path, 1)) && (path[0] != '/')) + if (arg_1 != VKI_AT_FDCWD && !ML_(fd_allowed)(arg_1, "mknodat", tid, False)) + SET_STATUS_Failure(VKI_EBADF); } // SYS_cpuset_getdomain 561 -- 2.47.3