]> git.ipfire.org Git - thirdparty/valgrind.git/commitdiff
FreeBSD syscalls: fd handling (part 3)
authorPaul Floyd <pjfloyd@wanadoo.fr>
Sat, 23 Aug 2025 13:42:47 +0000 (15:42 +0200)
committerPaul Floyd <pjfloyd@wanadoo.fr>
Sat, 23 Aug 2025 13:42:47 +0000 (15:42 +0200)
arm64 file

coregrind/m_syswrap/syswrap-arm64-freebsd.c
coregrind/m_syswrap/syswrap-x86-freebsd.c

index d90cf212ba6116e8e93a34c3084641dc891a938a..fe3107f50024bd9f5a6d124a6dfa183e0ab0e9b0 100644 (file)
@@ -202,20 +202,19 @@ PRE(sys_preadv)
          SARG1, ARG2, SARG3, SARG4);
    PRE_REG_READ4(ssize_t, "preadv", int, fd, const struct iovec*, iov, int,
                  iovcnt, vki_off_t, offset);
-   if (!ML_(fd_allowed)(ARG1, "preadv", tid, False)) {
+   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);
       }
    }
 }
@@ -259,17 +258,16 @@ PRE(sys_pwritev)
                  iovcnt, 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);
       }
    }
 }
@@ -287,7 +285,8 @@ PRE(sys_sendfile)
          SARG1, SARG2, ARG3, ARG4, ARG5, ARG6, SARG7);
    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));
    }
@@ -659,9 +658,8 @@ PRE(sys_pread)
 
    if (!ML_(fd_allowed)(ARG1, "read", 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)
@@ -721,6 +719,8 @@ PRE(sys_lseek)
          ARG1, ARG2, ARG3);
    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
@@ -741,6 +741,8 @@ PRE(sys_ftruncate)
    *flags |= SfMayBlock;
    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
@@ -814,6 +816,8 @@ PRE(sys_posix_fallocate)
          SARG1, ARG2, ARG3);
    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
@@ -825,7 +829,8 @@ PRE(sys_posix_fadvise)
          SARG1, ARG2, ARG3, SARG4);
    PRE_REG_READ4(long, "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_faadvise", tid, False))
+      SET_STATUS_Failure(VKI_EBADF);
 }
 
 // SYS_wait6   532
@@ -945,12 +950,17 @@ 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_READ4(long, "mknodat", int, fd, const char*, path, vki_mode_t, mode,
                  vki_dev_t, dev);
    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
index 421d7c5752e95e9997679fb0ec75ac76336e0282..5ea0524c76825302c2aeeb9f6ea717c72d64b6e7 100644 (file)
@@ -1388,8 +1388,8 @@ 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;
+   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))