Back to index

glibc  2.9
tst-fchownat.c
Go to the documentation of this file.
00001 #include <dirent.h>
00002 #include <fcntl.h>
00003 #include <stdio.h>
00004 #include <stdlib.h>
00005 #include <string.h>
00006 #include <unistd.h>
00007 
00008 
00009 static void prepare (void);
00010 #define PREPARE(argc, argv) prepare ()
00011 
00012 static int do_test (void);
00013 #define TEST_FUNCTION do_test ()
00014 
00015 #include "../test-skeleton.c"
00016 
00017 static int dir_fd;
00018 
00019 static void
00020 prepare (void)
00021 {
00022 #if _POSIX_CHOWN_RESTRICTED == 0
00023   if (pathconf (test_dir, _PC_CHOWN_RESTRICTED) != 0)
00024 #endif
00025     {
00026       uid_t uid = getuid ();
00027       if (uid != 0)
00028        {
00029          puts ("need root privileges");
00030          exit (0);
00031        }
00032     }
00033 
00034   size_t test_dir_len = strlen (test_dir);
00035   static const char dir_name[] = "/tst-fchownat.XXXXXX";
00036 
00037   size_t dirbuflen = test_dir_len + sizeof (dir_name);
00038   char *dirbuf = malloc (dirbuflen);
00039   if (dirbuf == NULL)
00040     {
00041       puts ("out of memory");
00042       exit (1);
00043     }
00044 
00045   snprintf (dirbuf, dirbuflen, "%s%s", test_dir, dir_name);
00046   if (mkdtemp (dirbuf) == NULL)
00047     {
00048       puts ("cannot create temporary directory");
00049       exit (1);
00050     }
00051 
00052   add_temp_file (dirbuf);
00053 
00054   dir_fd = open (dirbuf, O_RDONLY | O_DIRECTORY);
00055   if (dir_fd == -1)
00056     {
00057       puts ("cannot open directory");
00058       exit (1);
00059     }
00060 }
00061 
00062 
00063 static int
00064 do_test (void)
00065 {
00066   /* fdopendir takes over the descriptor, make a copy.  */
00067   int dupfd = dup (dir_fd);
00068   if (dupfd == -1)
00069     {
00070       puts ("dup failed");
00071       return 1;
00072     }
00073   if (lseek (dupfd, 0, SEEK_SET) != 0)
00074     {
00075       puts ("1st lseek failed");
00076       return 1;
00077     }
00078 
00079   /* The directory should be empty safe the . and .. files.  */
00080   DIR *dir = fdopendir (dupfd);
00081   if (dir == NULL)
00082     {
00083       puts ("fdopendir failed");
00084       return 1;
00085     }
00086   struct dirent64 *d;
00087   while ((d = readdir64 (dir)) != NULL)
00088     if (strcmp (d->d_name, ".") != 0 && strcmp (d->d_name, "..") != 0)
00089       {
00090        printf ("temp directory contains file \"%s\"\n", d->d_name);
00091        return 1;
00092       }
00093   closedir (dir);
00094 
00095   /* Try to create a file.  */
00096   int fd = openat (dir_fd, "some-file", O_CREAT|O_RDWR|O_EXCL, 0666);
00097   if (fd == -1)
00098     {
00099       if (errno == ENOSYS)
00100        {
00101          puts ("*at functions not supported");
00102          return 0;
00103        }
00104 
00105       puts ("file creation failed");
00106       return 1;
00107     }
00108   write (fd, "hello", 5);
00109   puts ("file created");
00110 
00111   struct stat64 st1;
00112   if (fstat64 (fd, &st1) != 0)
00113     {
00114       puts ("fstat64 failed");
00115       return 1;
00116     }
00117 
00118   /* Before closing the file, try using this file descriptor to open
00119      another file.  This must fail.  */
00120   if (fchownat (fd, "some-file", 1, 1, 0) != -1)
00121     {
00122       puts ("fchownat using descriptor for normal file worked");
00123       return 1;
00124     }
00125   if (errno != ENOTDIR)
00126     {
00127       puts ("\
00128 error for fchownat using descriptor for normal file not ENOTDIR ");
00129       return 1;
00130     }
00131 
00132   close (fd);
00133 
00134   if (fchownat (dir_fd, "some-file", st1.st_uid + 1, st1.st_gid + 1, 0) != 0)
00135     {
00136       puts ("fchownat failed");
00137       return 1;
00138     }
00139 
00140   struct stat64 st2;
00141   if (fstatat64 (dir_fd, "some-file", &st2, 0) != 0)
00142     {
00143       puts ("fstatat64 failed");
00144       return 1;
00145     }
00146 
00147   if (st1.st_uid + 1 != st2.st_uid || st1.st_gid + 1 != st2.st_gid)
00148     {
00149       puts ("owner change failed");
00150       return 1;
00151     }
00152 
00153   if (unlinkat (dir_fd, "some-file", 0) != 0)
00154     {
00155       puts ("unlinkat failed");
00156       return 1;
00157     }
00158 
00159   /* Create a file descriptor which is closed again right away.  */
00160   int dir_fd2 = dup (dir_fd);
00161   if (dir_fd2 == -1)
00162     {
00163       puts ("dup failed");
00164       return 1;
00165     }
00166   close (dir_fd2);
00167 
00168   if (fchownat (dir_fd2, "some-file", 1, 1, 0) != -1)
00169     {
00170       puts ("fchownat using closed descriptor worked");
00171       return 1;
00172     }
00173   if (errno != EBADF)
00174     {
00175       puts ("error for fchownat using closed descriptor not EBADF ");
00176       return 1;
00177     }
00178 
00179   close (dir_fd);
00180 
00181   if (fchownat (-1, "some-file", 1, 1, 0) != -1)
00182     {
00183       puts ("fchownat using invalid descriptor worked");
00184       return 1;
00185     }
00186   if (errno != EBADF)
00187     {
00188       puts ("error for fchownat using invalid descriptor not EBADF ");
00189       return 1;
00190     }
00191 
00192   return 0;
00193 }