Bug 307103 - sys_openat If pathname is absolute, then dirfd is ignored.
Summary: sys_openat If pathname is absolute, then dirfd is ignored.
Status: RESOLVED FIXED
Alias: None
Product: valgrind
Classification: Developer tools
Component: general (show other bugs)
Version: 3.9.0.SVN
Platform: unspecified Linux
: NOR normal
Target Milestone: ---
Assignee: Julian Seward
URL:
Keywords:
: 331744 337716 (view as bug list)
Depends on:
Blocks:
 
Reported: 2012-09-20 14:16 UTC by Mark Wielaard
Modified: 2014-10-20 22:13 UTC (History)
2 users (show)

See Also:
Latest Commit:
Version Fixed In:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Mark Wielaard 2012-09-20 14:16:49 UTC
For sys-openat the dirfd argument should be ignored when the pathname is absolute.

Example:

#define _GNU_SOURCE
#include <fcntl.h>
#include <unistd.h>

int
main (void)
{
  int dfd = open ("/tmp", O_RDONLY);
  int fd1 = openat (dfd, "abc", O_RDONLY);
  /* This is fine, absolute path. */
  int fd2 = openat (0x12345678, "/tmp/abc", O_RDONLY);
  int fd3 = openat (AT_FDCWD, "abc", O_RDONLY);
  /* This is the only one that should warn.  */
  int fd4 = openat (0x12345678, "abc", O_RDONLY);
  return 0;
}

Patch:

--- valgrind-3.8.1/coregrind/m_syswrap/syswrap-linux.c.jj	2007-12-11 00:18:43.000000000 +0100
+++ valgrind-3.8.1/coregrind/m_syswrap/syswrap-linux.c	2008-03-03 11:35:15.000000000 +0100
@@ -3308,10 +3308,15 @@ PRE(sys_openat)
                     int, dfd, const char *, filename, int, flags);
    }
 
-   if (ARG1 != VKI_AT_FDCWD && !ML_(fd_allowed)(ARG1, "openat", tid, False))
+   PRE_MEM_RASCIIZ( "openat(filename)", ARG2 );
+
+   /* For absolute filenames, dfd is ignored.  If dfd is AT_FDCWD,
+      filename is relative to cwd.  */
+   if (ML_(safe_to_deref)( (void*)ARG2, 1 )
+       && *(Char *)ARG2 != '/'
+       && ARG1 != VKI_AT_FDCWD
+       && !ML_(fd_allowed)(ARG1, "openat", tid, False))
       SET_STATUS_Failure( VKI_EBADF );
-   else
-      PRE_MEM_RASCIIZ( "openat(filename)", ARG2 );
 
    /* Handle the case where the open is of /proc/self/cmdline or
       /proc/<pid>/cmdline, and just give it a copy of the fd for the


Reproducible: Always

Steps to Reproduce:
1. gcc -g -o openat openat.c
2. valgrind ./openat
Actual Results:  
Two warnings:
==25223== Warning: invalid file descriptor 305419896 in syscall openat()
==25223== Warning: invalid file descriptor 305419896 in syscall openat()


Expected Results:  
Just one warning:
==25223== Warning: invalid file descriptor 305419896 in syscall openat()



This is fedora valgrind.spec valgrind-3.8.1-openat.patch
Comment 1 Julian Seward 2012-12-06 16:05:37 UTC
Committed, r13159.  Thanks.
Comment 2 Philippe Waroquiers 2014-04-03 21:30:43 UTC
*** Bug 331744 has been marked as a duplicate of this bug. ***
Comment 3 Florian Krohm 2014-10-20 22:13:11 UTC
*** Bug 337716 has been marked as a duplicate of this bug. ***