diff mbox series

Test mkdirat use of mode argument

Message ID c9365e5-a5ef-d829-a7c-4911883155a8@redhat.com
State New
Headers show
Series Test mkdirat use of mode argument | expand

Commit Message

Joseph Myers Aug. 21, 2024, 9:14 p.m. UTC
The test io/tst-mkdirat doesn't verify the permissions on the created
directory (thus, doesn't verify at all anything about how mkdirat uses
the mode argument).  Add checks of this to the existing test.

Tested for x86_64.

Comments

Andreas Schwab Aug. 22, 2024, 6:55 a.m. UTC | #1
On Aug 21 2024, Joseph Myers wrote:

> The test io/tst-mkdirat doesn't verify the permissions on the created
> directory (thus, doesn't verify at all anything about how mkdirat uses
> the mode argument).  Add checks of this to the existing test.

Ok.
Florian Weimer Aug. 22, 2024, 7:39 a.m. UTC | #2
* Joseph Myers:

> The test io/tst-mkdirat doesn't verify the permissions on the created
> directory (thus, doesn't verify at all anything about how mkdirat uses
> the mode argument).  Add checks of this to the existing test.

I think this is where the FUSE test framework can help.  The kernel
passes umask and mode argument separately:

struct fuse_mkdir_in {
	uint32_t	mode;
	uint32_t	umask;
};

So it wouldn't be necessary to model the interaction between mode and
umask.

This patch looks okay, too.

Reviewed-by: Florian Weimer <fweimer@redhat.com>

Thanks,
Florian
diff mbox series

Patch

diff --git a/io/tst-mkdirat.c b/io/tst-mkdirat.c
index 605e51ef1e..b97bc3ca6d 100644
--- a/io/tst-mkdirat.c
+++ b/io/tst-mkdirat.c
@@ -53,6 +53,10 @@  prepare (void)
 static int
 do_test (void)
 {
+  /* Find the current umask.  */
+  mode_t mask = umask (022);
+  umask (mask);
+
   /* fdopendir takes over the descriptor, make a copy.  */
   int dupfd = dup (dir_fd);
   if (dupfd == -1)
@@ -107,6 +111,13 @@  do_test (void)
       puts ("mkdirat did not create a directory");
       return 1;
     }
+  if ((st1.st_mode & 01777) != (~mask & 0777))
+    {
+      printf ("mkdirat created directory with wrong mode %o, expected %o\n",
+	      (unsigned int) (st1.st_mode & 01777),
+	      (unsigned int) (~mask & 0777));
+      return 1;
+    }
 
   dupfd = dup (dir_fd);
   if (dupfd == -1)
@@ -156,6 +167,37 @@  do_test (void)
       return 1;
     }
 
+  /* Test again with a different mode.  */
+  umask (0);
+  e = mkdirat (dir_fd, "some-dir", 01755);
+  umask (mask);
+  if (e == -1)
+    {
+      puts ("directory creation (different mode) failed");
+      return 1;
+    }
+  if (fstatat64 (dir_fd, "some-dir", &st1, 0) != 0)
+    {
+      puts ("fstat64 (different mode) failed");
+      return 1;
+    }
+  if (!S_ISDIR (st1.st_mode))
+    {
+      puts ("mkdirat (different mode) did not create a directory");
+      return 1;
+    }
+  if ((st1.st_mode & 01777) != 01755)
+    {
+      printf ("mkdirat (different mode) created directory with wrong mode %o\n",
+	      (unsigned int) (st1.st_mode & 01777));
+      return 1;
+    }
+  if (unlinkat (dir_fd, "some-dir", AT_REMOVEDIR) != 0)
+    {
+      puts ("unlinkat (different mode) failed");
+      return 1;
+    }
+
   close (dir_fd);
 
   return 0;