Blame posix/tst-chmod.c

Packit 6c4009
/* Test for chmod functions.
Packit 6c4009
   Copyright (C) 2000-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
   Contributed by Ulrich Drepper <drepper@cygnus.com>, 2000.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <dirent.h>
Packit 6c4009
#include <errno.h>
Packit 6c4009
#include <error.h>
Packit 6c4009
#include <fcntl.h>
Packit 6c4009
#include <mcheck.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <unistd.h>
Packit 6c4009
#include <sys/stat.h>
Packit 6c4009
Packit 6c4009
Packit 6c4009
#define OUT_OF_MEMORY \
Packit 6c4009
  do {									      \
Packit 6c4009
    puts ("cannot allocate memory");					      \
Packit 6c4009
    result = 1;								      \
Packit 6c4009
    goto fail;								      \
Packit 6c4009
  } while (0)
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
do_test (int argc, char *argv[])
Packit 6c4009
{
Packit 6c4009
  const char *builddir;
Packit 6c4009
  struct stat64 st1;
Packit 6c4009
  struct stat64 st2;
Packit 6c4009
  char *buf;
Packit 6c4009
  char *testdir;
Packit 6c4009
  char *testfile = NULL;
Packit 6c4009
  char *startdir;
Packit 6c4009
  size_t buflen;
Packit 6c4009
  int fd;
Packit 6c4009
  int result = 0;
Packit 6c4009
  DIR *dir;
Packit 6c4009
Packit 6c4009
  mtrace ();
Packit 6c4009
Packit 6c4009
  if (argc <= 1)
Packit 6c4009
    error (EXIT_FAILURE, 0, "no parameters");
Packit 6c4009
Packit 6c4009
  /* This is where we will create the test files.  */
Packit 6c4009
  builddir = argv[1];
Packit 6c4009
  buflen = strlen (builddir) + 50;
Packit 6c4009
Packit 6c4009
  startdir = getcwd (NULL, 0);
Packit 6c4009
  if (startdir == NULL)
Packit 6c4009
    {
Packit 6c4009
      printf ("cannot get current directory: %m\n");
Packit 6c4009
      exit (EXIT_FAILURE);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* A buffer large enough for everything we need.  */
Packit 6c4009
  buf = (char *) alloca (buflen);
Packit 6c4009
Packit 6c4009
  /* Create the directory name.  */
Packit 6c4009
  snprintf (buf, buflen, "%s/chmoddirXXXXXX", builddir);
Packit 6c4009
Packit 6c4009
  if (mkdtemp (buf) == NULL)
Packit 6c4009
    {
Packit 6c4009
      printf ("cannot create test directory: %m\n");
Packit 6c4009
      exit (EXIT_FAILURE);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (chmod ("", 0600) == 0)
Packit 6c4009
    {
Packit 6c4009
      puts ("chmod(\"\", 0600 didn't fail");
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
  else if (errno != ENOENT)
Packit 6c4009
    {
Packit 6c4009
      puts ("chmod(\"\",0600) does not set errno to ENOENT");
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Create a duplicate.  */
Packit 6c4009
  testdir = strdup (buf);
Packit 6c4009
  if (testdir == NULL)
Packit 6c4009
    OUT_OF_MEMORY;
Packit 6c4009
Packit 6c4009
  if (stat64 (testdir, &st1) != 0)
Packit 6c4009
    {
Packit 6c4009
      printf ("cannot stat test directory: %m\n");
Packit 6c4009
      exit (1);
Packit 6c4009
    }
Packit 6c4009
  if (!S_ISDIR (st1.st_mode))
Packit 6c4009
    {
Packit 6c4009
      printf ("file not created as directory: %m\n");
Packit 6c4009
      exit (1);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* We have to wait for a second to make sure the ctime changes.  */
Packit 6c4009
  sleep (1);
Packit 6c4009
Packit 6c4009
  /* Remove all access rights from the directory.  */
Packit 6c4009
  if (chmod (testdir, 0) != 0)
Packit 6c4009
    {
Packit 6c4009
      printf ("cannot change mode of test directory: %m\n");
Packit 6c4009
      result = 1;
Packit 6c4009
      goto fail;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (stat64 (testdir, &st2) != 0)
Packit 6c4009
    {
Packit 6c4009
      printf ("cannot stat test directory: %m\n");
Packit 6c4009
      result = 1;
Packit 6c4009
      goto fail;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Compare result.  */
Packit 6c4009
  if ((st2.st_mode & ALLPERMS) != 0)
Packit 6c4009
    {
Packit 6c4009
      printf ("chmod(...,0) on directory left bits nonzero: %o\n",
Packit 6c4009
	      st2.st_mode & ALLPERMS);
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
  if (st1.st_ctime >= st2.st_ctime)
Packit 6c4009
    {
Packit 6c4009
      puts ("chmod(...,0) did not set ctime correctly");
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Name of a file in the directory.  */
Packit 6c4009
  snprintf (buf, buflen, "%s/file", testdir);
Packit 6c4009
  testfile = strdup (buf);
Packit 6c4009
  if (testfile == NULL)
Packit 6c4009
    OUT_OF_MEMORY;
Packit 6c4009
Packit 6c4009
  fd = creat (testfile, 0);
Packit 6c4009
  if (fd != -1)
Packit 6c4009
    {
Packit 6c4009
      if (getuid () != 0)
Packit 6c4009
	{
Packit 6c4009
	  puts ("managed to create test file in protected directory");
Packit 6c4009
	  result = 1;
Packit 6c4009
	}
Packit 6c4009
      close (fd);
Packit 6c4009
    }
Packit 6c4009
  else if (errno != EACCES)
Packit 6c4009
    {
Packit 6c4009
      puts ("creat didn't generate correct errno value");
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* With this mode it still shouldn't be possible to create a file.  */
Packit 6c4009
  if (chmod (testdir, 0600) != 0)
Packit 6c4009
    {
Packit 6c4009
      printf ("cannot change mode of test directory to 0600: %m\n");
Packit 6c4009
      result = 1;
Packit 6c4009
      goto fail;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  fd = creat (testfile, 0);
Packit 6c4009
  if (fd != -1)
Packit 6c4009
    {
Packit 6c4009
      if (getuid () != 0)
Packit 6c4009
	{
Packit 6c4009
	  puts ("managed to create test file in no-x protected directory");
Packit 6c4009
	  result = 1;
Packit 6c4009
	}
Packit 6c4009
      close (fd);
Packit 6c4009
    }
Packit 6c4009
  else if (errno != EACCES)
Packit 6c4009
    {
Packit 6c4009
      puts ("creat didn't generate correct errno value");
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Change the directory mode back to allow creating a file.  This
Packit 6c4009
     time with fchmod.  */
Packit 6c4009
  dir = opendir (testdir);
Packit 6c4009
  if (dir != NULL)
Packit 6c4009
    {
Packit 6c4009
      if (fchmod (dirfd (dir), 0700) != 0)
Packit 6c4009
	{
Packit 6c4009
	  printf ("cannot change mode of test directory to 0700: %m\n");
Packit 6c4009
	  result = 1;
Packit 6c4009
	  closedir (dir);
Packit 6c4009
	  goto fail;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      closedir (dir);
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
      printf ("cannot open directory: %m\n");
Packit 6c4009
      result = 1;
Packit 6c4009
Packit 6c4009
      if (chmod (testdir, 0700) != 0)
Packit 6c4009
	{
Packit 6c4009
	  printf ("cannot change mode of test directory to 0700: %m\n");
Packit 6c4009
	  goto fail;
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  fd = creat (testfile, 0);
Packit 6c4009
  if (fd == -1)
Packit 6c4009
    {
Packit 6c4009
      puts ("still didn't manage to create test file in protected directory");
Packit 6c4009
      result = 1;
Packit 6c4009
      goto fail;
Packit 6c4009
    }
Packit 6c4009
  if (fstat64 (fd, &st1) != 0)
Packit 6c4009
    {
Packit 6c4009
      printf ("cannot stat new file: %m\n");
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
  else if ((st1.st_mode & ALLPERMS) != 0)
Packit 6c4009
    {
Packit 6c4009
      puts ("file not created with access mode 0");
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
  close (fd);
Packit 6c4009
Packit 6c4009
  snprintf (buf, buflen, "%s/..", testdir);
Packit 6c4009
  chdir (buf);
Packit 6c4009
  /* We are now in the directory above the one we create the test
Packit 6c4009
     directory in.  */
Packit 6c4009
Packit 6c4009
  sleep (1);
Packit 6c4009
  snprintf (buf, buflen, "./%s/../%s/file",
Packit 6c4009
	    basename (testdir), basename (testdir));
Packit 6c4009
  if (chmod (buf, 0600) != 0)
Packit 6c4009
    {
Packit 6c4009
      printf ("cannot change mode of file to 0600: %m\n");
Packit 6c4009
      result = 1;
Packit 6c4009
      goto fail;
Packit 6c4009
    }
Packit 6c4009
  snprintf (buf, buflen, "./%s//file", basename (testdir));
Packit 6c4009
  if (stat64 (buf, &st2) != 0)
Packit 6c4009
    {
Packit 6c4009
      printf ("cannot stat new file: %m\n");
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
  else if ((st2.st_mode & ALLPERMS) != 0600)
Packit 6c4009
    {
Packit 6c4009
      puts ("file mode not changed to 0600");
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
  else if (st1.st_ctime >= st2.st_ctime)
Packit 6c4009
    {
Packit 6c4009
      puts ("chmod(\".../file\",0600) did not set ctime correctly");
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (chmod (buf, 0777 | S_ISUID | S_ISGID) != 0)
Packit 6c4009
    {
Packit 6c4009
      printf ("cannot change mode of file to %o: %m\n",
Packit 6c4009
	      0777 | S_ISUID | S_ISGID);
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
  if (stat64 (buf, &st2) != 0)
Packit 6c4009
    {
Packit 6c4009
      printf ("cannot stat test file: %m\n");
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
  else if ((st2.st_mode & ALLPERMS) != (0777 | S_ISUID | S_ISGID))
Packit 6c4009
    {
Packit 6c4009
      puts ("file mode not changed to 0777 | S_ISUID | S_ISGID");
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (chmod (basename (testdir), 0777 | S_ISUID | S_ISGID | S_ISVTX) != 0)
Packit 6c4009
    {
Packit 6c4009
      printf ("cannot change mode of test directory to %o: %m\n",
Packit 6c4009
	      0777 | S_ISUID | S_ISGID | S_ISVTX);
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
  if (stat64 (basename (testdir), &st2) != 0)
Packit 6c4009
    {
Packit 6c4009
      printf ("cannot stat test directory: %m\n");
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
  else if ((st2.st_mode & ALLPERMS) != (0777 | S_ISUID | S_ISGID | S_ISVTX))
Packit 6c4009
    {
Packit 6c4009
      puts ("directory mode not changed to 0777 | S_ISUID | S_ISGID | S_ISGID");
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  snprintf (buf, buflen, "./%s/no-such-file", basename (testdir));
Packit 6c4009
  if (chmod (buf, 0600) != -1)
Packit 6c4009
    {
Packit 6c4009
      puts ("chmod(\".../no-such-file\",0600) did not fail");
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
  else if (errno != ENOENT)
Packit 6c4009
    {
Packit 6c4009
      puts ("chmod(\".../no-such-file\",0600) does not set errno to ENOENT");
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  snprintf (buf, buflen, "%s/", basename (testdir));
Packit 6c4009
  if (chmod (basename (testdir), 0677) != 0)
Packit 6c4009
    {
Packit 6c4009
      printf ("cannot change mode of test directory to 0677: %m\n");
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
      snprintf (buf, buflen, "./%s/file", basename (testdir));
Packit 6c4009
      if (chmod (buf, 0600) == 0)
Packit 6c4009
	{
Packit 6c4009
	  if (getuid () != 0)
Packit 6c4009
	    {
Packit 6c4009
	      puts ("chmod(\".../file\") with no-exec directory succeeded");
Packit 6c4009
	      result = 1;
Packit 6c4009
	    }
Packit 6c4009
	}
Packit 6c4009
      else if (errno != EACCES)
Packit 6c4009
	{
Packit 6c4009
	  puts ("chmod(\".../file\") with no-exec directory didn't set EACCES");
Packit 6c4009
	  result = 1;
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (chmod (basename (testdir), 0777) != 0)
Packit 6c4009
    {
Packit 6c4009
      printf ("cannot change mode of test directory to 0777: %m\n");
Packit 6c4009
      result = 1;
Packit 6c4009
      goto fail;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  snprintf (buf, buflen, "%s/file/cannot-be", basename (testdir));
Packit 6c4009
  if (chmod (buf, 0600) == 0)
Packit 6c4009
    {
Packit 6c4009
      puts ("chmod(\".../file/cannot-be\",0600) did not fail");
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
  else if (errno != ENOTDIR)
Packit 6c4009
    {
Packit 6c4009
      puts ("chmod(\".../file/cannot-be\",0600) does not set errno to ENOTDIR");
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
 fail:
Packit 6c4009
  chdir (startdir);
Packit 6c4009
Packit 6c4009
  /* Remove all the files.  */
Packit 6c4009
  chmod (testdir, 0700);
Packit 6c4009
  if (testfile != NULL)
Packit 6c4009
    {
Packit 6c4009
      chmod (testfile, 0700);
Packit 6c4009
      unlink (testfile);
Packit 6c4009
    }
Packit 6c4009
  rmdir (testdir);
Packit 6c4009
Packit 6c4009
  /* Free the resources.  */
Packit 6c4009
  free (testfile);
Packit 6c4009
  free (testdir);
Packit 6c4009
  free (startdir);
Packit 6c4009
Packit 6c4009
  return result;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* We need a few seconds since we have a few sleeps in the code.  */
Packit 6c4009
#define TIMEOUT	20
Packit 6c4009
Packit 6c4009
Packit 6c4009
#include "../test-skeleton.c"