]> git.ipfire.org Git - thirdparty/valgrind.git/commitdiff
illumos syscalls: refactor *at directory fd checks
authorPaul Floyd <pjfloyd@wanadoo.fr>
Sun, 31 Aug 2025 06:02:00 +0000 (08:02 +0200)
committerPaul Floyd <pjfloyd@wanadoo.fr>
Sun, 31 Aug 2025 06:02:00 +0000 (08:02 +0200)
Mainly to reduyce the amount of copied and pasted code.
Slight improvements to messages to syscalls that have two
directory fds.

coregrind/m_syswrap/syswrap-solaris.c
memcheck/tests/solaris/syscall_at.stderr.exp

index f620e85bd7aa01999f78bbfdb69ef3f77d27ac8b..a4f243f8adc20a3725eca156a2b87da209394d6d 100644 (file)
@@ -1812,18 +1812,8 @@ PRE(sys_linkat)
    PRE_MEM_RASCIIZ("linkat(path2)", ARG4);
 
    /* Be strict but ignore fd1/fd2 for absolute path1/path2. */
-   if (fd1 != VKI_AT_FDCWD
-       && ML_(safe_to_deref)((void *) ARG2, 1)
-       && ((HChar *) ARG2)[0] != '/'
-       && !ML_(fd_allowed)(fd1, "linkat", tid, False)) {
-      SET_STATUS_Failure(VKI_EBADF);
-   }
-   if (fd2 != VKI_AT_FDCWD
-       && ML_(safe_to_deref)((void *) ARG4, 1)
-       && ((HChar *) ARG4)[0] != '/'
-       && !ML_(fd_allowed)(fd2, "linkat", tid, False)) {
-      SET_STATUS_Failure(VKI_EBADF);
-   }
+   ML_(fd_at_check_allowed)(fd1, (const HChar*)ARG2, "linkat(efd)", tid, status);
+   ML_(fd_at_check_allowed)(fd2, (const HChar*)ARG4, "linkat(nfd)", tid, status);
 
    *flags |= SfMayBlock;
 }
@@ -1844,11 +1834,7 @@ PRE(sys_symlinkat)
    PRE_MEM_RASCIIZ("symlinkat(path2)", ARG3);
 
    /* Be strict but ignore fd for absolute path2. */
-   if (fd != VKI_AT_FDCWD
-       && ML_(safe_to_deref)((void *) ARG3, 1)
-       && ((HChar *) ARG3)[0] != '/'
-       && !ML_(fd_allowed)(fd, "symlinkat", tid, False))
-      SET_STATUS_Failure(VKI_EBADF);
+   ML_(fd_at_check_allowed)(fd, (const HChar*)ARG3, "symlinkat", tid, status);
 
    *flags |= SfMayBlock;
 }
@@ -2323,11 +2309,7 @@ PRE(sys_readlinkat)
    PRE_MEM_WRITE("readlinkat(buf)", ARG3, ARG4);
 
    /* Be strict but ignore dfd for absolute path. */
-   if (dfd != VKI_AT_FDCWD
-       && ML_(safe_to_deref)((void *) ARG2, 1)
-       && ((HChar *) ARG2)[0] != '/'
-       && !ML_(fd_allowed)(dfd, "readlinkat", tid, False))
-      SET_STATUS_Failure(VKI_EBADF);
+   ML_(fd_at_check_allowed)(dfd, (const HChar*)ARG2, "readlinkat", tid, status);
 
    /* Handle the case where readlinkat is looking at /proc/self/path/a.out or
       /proc/<pid>/path/a.out. */
@@ -2386,11 +2368,7 @@ PRE(sys_frealpathat)
    PRE_MEM_WRITE("frealpathat(buf)", ARG3, ARG4);
 
    /* Be strict but ignore fd for absolute path. */
-   if (fd != VKI_AT_FDCWD
-       && ML_(safe_to_deref)((void *) ARG2, 1)
-       && ((HChar *) ARG2)[0] != '/'
-       && !ML_(fd_allowed)(fd, "frealpathat", tid, False))
-      SET_STATUS_Failure(VKI_EBADF);
+   ML_(fd_at_check_allowed)(fd, (const HChar*)ARG2, "frealpathat", tid, status);
 }
 
 POST(sys_frealpathat)
@@ -2529,11 +2507,7 @@ PRE(sys_faccessat)
    PRE_MEM_RASCIIZ("faccessat(path)", ARG2);
 
    /* Be strict but ignore fd for absolute path. */
-   if (fd != VKI_AT_FDCWD
-       && ML_(safe_to_deref)((void *) ARG2, 1)
-       && ((HChar *) ARG2)[0] != '/'
-       && !ML_(fd_allowed)(fd, "faccessat", tid, False))
-      SET_STATUS_Failure(VKI_EBADF);
+   ML_(fd_at_check_allowed)(fd, (const HChar*)ARG2, "faccessat", tid, status);
 }
 
 PRE(sys_mknodat)
@@ -2551,11 +2525,7 @@ PRE(sys_mknodat)
    PRE_MEM_RASCIIZ("mknodat(fname)", ARG2);
 
    /* Be strict but ignore fd for absolute path. */
-   if (fd != VKI_AT_FDCWD
-       && ML_(safe_to_deref)((void *) ARG2, 1)
-       && ((HChar *) ARG2)[0] != '/'
-       && !ML_(fd_allowed)(fd, "mknodat", tid, False))
-      SET_STATUS_Failure(VKI_EBADF);
+   ML_(fd_at_check_allowed)(fd, (const HChar*)ARG2, "mknodat", tid, status);
 
    *flags |= SfMayBlock;
 }
@@ -3593,11 +3563,7 @@ PRE(sys_fchownat)
       PRE_MEM_RASCIIZ("fchownat(path)", ARG2);
 
    /* Be strict but ignore fd for absolute path. */
-   if (fd != VKI_AT_FDCWD
-       && ML_(safe_to_deref)((void *) ARG2, 1)
-       && ((HChar *) ARG2)[0] != '/'
-       && !ML_(fd_allowed)(fd, "fchownat", tid, False))
-      SET_STATUS_Failure(VKI_EBADF);
+   ML_(fd_at_check_allowed)(fd, (const HChar*)ARG2, "fchownat", tid, status);
 }
 
 PRE(sys_fdsync)
@@ -4141,18 +4107,8 @@ PRE(sys_renameat)
    PRE_MEM_RASCIIZ("renameat(new)", ARG4);
 
    /* Be strict but ignore fromfd/tofd for absolute old/new. */
-   if (fromfd != VKI_AT_FDCWD
-       && ML_(safe_to_deref)((void *) ARG2, 1)
-       && ((HChar *) ARG2)[0] != '/'
-       && !ML_(fd_allowed)(fromfd, "renameat", tid, False)) {
-      SET_STATUS_Failure(VKI_EBADF);
-   }
-   if (tofd != VKI_AT_FDCWD
-       && ML_(safe_to_deref)((void *) ARG4, 1)
-       && ((HChar *) ARG4)[0] != '/'
-       && !ML_(fd_allowed)(tofd, "renameat", tid, False)) {
-      SET_STATUS_Failure(VKI_EBADF);
-   }
+   ML_(fd_at_check_allowed)(fromfd, (const HChar*)ARG2, "renameat(fromfd)", tid, status);
+   ML_(fd_at_check_allowed)(tofd, (const HChar*)ARG4, "renameat(tofd)", tid, status);
 }
 
 PRE(sys_unlinkat)
@@ -4171,11 +4127,7 @@ PRE(sys_unlinkat)
    PRE_MEM_RASCIIZ("unlinkat(pathname)", ARG2);
 
    /* Be strict but ignore dfd for absolute pathname. */
-   if (dfd != VKI_AT_FDCWD
-       && ML_(safe_to_deref)((void *) ARG2, 1)
-       && ((HChar *) ARG2)[0] != '/'
-       && !ML_(fd_allowed)(dfd, "unlinkat", tid, False))
-      SET_STATUS_Failure(VKI_EBADF);
+   ML_(fd_at_check_allowed)(dfd, (const HChar*)ARG2, "unlinkat", tid, status);
 }
 
 PRE(sys_fstatat)
@@ -4199,11 +4151,7 @@ PRE(sys_fstatat)
    PRE_MEM_WRITE("fstatat(buf)", ARG3, sizeof(struct vki_stat));
 
    /* Be strict but ignore fildes for absolute path. */
-   if (fd != VKI_AT_FDCWD
-       && ML_(safe_to_deref)((void *) ARG2, 1)
-       && ((HChar *) ARG2)[0] != '/'
-       && !ML_(fd_allowed)(fd, "fstatat", tid, False))
-      SET_STATUS_Failure(VKI_EBADF);
+   ML_(fd_at_check_allowed)(fd, (const HChar*)ARG2, "fstatat", tid, status);
 }
 
 POST(sys_fstatat)
@@ -4237,6 +4185,7 @@ PRE(sys_openat)
 
    PRE_MEM_RASCIIZ("openat(filename)", ARG2);
 
+   // @todo PJF use ML_(fd_at_check) and not return early here
    /* Be strict but ignore fildes for absolute pathname. */
    if (fd != VKI_AT_FDCWD
        && ML_(safe_to_deref)((void *) ARG2, 1)
@@ -5055,11 +5004,7 @@ PRE(sys_fchmodat)
       PRE_MEM_RASCIIZ("fchmodat(path)", ARG2);
 
    /* Be strict but ignore fd for absolute path. */
-   if (fd != VKI_AT_FDCWD
-       && ML_(safe_to_deref)((void *) ARG2, 1)
-       && ((HChar *) ARG2)[0] != '/'
-       && !ML_(fd_allowed)(fd, "fchmodat", tid, False))
-      SET_STATUS_Failure(VKI_EBADF);
+   ML_(fd_at_check_allowed)(fd, (const HChar*)ARG2, "fchmodat", tid, status);
 }
 
 PRE(sys_mkdirat)
@@ -5077,11 +5022,7 @@ PRE(sys_mkdirat)
    PRE_MEM_RASCIIZ("mkdirat(path)", ARG2);
 
    /* Be strict but ignore fd for absolute path. */
-   if (fd != VKI_AT_FDCWD
-       && ML_(safe_to_deref)((void *) ARG2, 1)
-       && ((HChar *) ARG2)[0] != '/'
-       && !ML_(fd_allowed)(fd, "mkdirat", tid, False))
-      SET_STATUS_Failure(VKI_EBADF);
+   ML_(fd_at_check_allowed)(fd, (const HChar*)ARG2, "mkdirat", tid, status);
 }
 
 static void do_statvfs_post(struct vki_statvfs *stats, ThreadId tid)
@@ -5273,11 +5214,7 @@ PRE(sys_utimesys)
             PRE_MEM_READ("utimesys(times)", ARG4, 2 * sizeof(vki_timespec_t));
 
          /* Be strict but ignore fd for absolute path. */
-         if (fd != VKI_AT_FDCWD
-             && ML_(safe_to_deref)((void *) ARG3, 1)
-             && ((HChar *) ARG3)[0] != '/'
-             && !ML_(fd_allowed)(fd, "utimesys", tid, False))
-            SET_STATUS_Failure(VKI_EBADF);
+         ML_(fd_at_check_allowed)(fd, (const HChar*)ARG3, "utimesys", tid, status);
          break;
       }
    default:
@@ -5309,11 +5246,7 @@ PRE(sys_utimensat)
       PRE_MEM_READ("utimensat(times)", ARG3, 2 * sizeof(vki_timespec_t));
 
    /* Be strict but ignore fd for absolute path. */
-   if (fd != VKI_AT_FDCWD
-       && ML_(safe_to_deref)((void *) ARG2, 1)
-       && ((HChar *) ARG2)[0] != '/'
-       && !ML_(fd_allowed)(fd, "utimensat", tid, False))
-      SET_STATUS_Failure(VKI_EBADF);
+   ML_(fd_at_check_allowed)(fd, (const HChar*)ARG2, "utimensat", tid, status);
 }
 #endif /* SOLARIS_UTIMENSAT_SYSCALL */
 
index b49fda289fc9e92ff267c2eaeef6e6b0a58519b0..59815bfa9839d7e5814b2f00579e5f3f57e56901 100644 (file)
@@ -1,12 +1,12 @@
 
-Warning: invalid file descriptor 159879507 in syscall linkat()
-Warning: invalid file descriptor 159879508 in syscall linkat()
+Warning: invalid file descriptor 159879507 in syscall linkat(efd)()
+Warning: invalid file descriptor 159879508 in syscall linkat(nfd)()
 Warning: invalid file descriptor 646349138 in syscall symlinkat()
 Warning: invalid file descriptor 70680914 in syscall readlinkat()
 Warning: invalid file descriptor 68362578 in syscall faccessat()
 Warning: invalid file descriptor 70685266 in syscall fchownat()
-Warning: invalid file descriptor 70717779 in syscall renameat()
-Warning: invalid file descriptor 70717780 in syscall renameat()
+Warning: invalid file descriptor 70717779 in syscall renameat(fromfd)()
+Warning: invalid file descriptor 70717780 in syscall renameat(tofd)()
 Warning: invalid file descriptor 123765074 in syscall unlinkat()
 Warning: invalid file descriptor 1112625490 in syscall fstatat()
 Warning: invalid file descriptor 151224658 in syscall openat()