Skip to contentSkip to conversation

ext2: Avoid creating new file in append-only dir when open(2) return error

PATCH New Eryu Guan 1 participant 1 message Oct 28, 2011 ext2 file system

lore
Share & tools
lore

Eryu Guan

author
#0 lore raw

PATCH 1/1 ext2: Avoid creating new file in append-only dir when open(2) return error

Newly created file on ext2 inherits inode flags from parent directory, so new inode created in append-only directory has S_APPEND flag set, may_open() called by do_last() checks that flag then returns -EPERM, but at that time the new inode is already created.

This can be reproduced by:

	# mkdir -p /mnt/ext2/append-only
	# chattr +a /mnt/ext2/append-only
	# ./opentest /mnt/ext2/append-only/newtestfile
	# ls -l /mnt/ext2/append-only/newtestfile

opentest will return 'Operation not permitted', but the ls shows that newtestfile is already created.

	# cat opentest.c
	#include <stdio.h>
	#include <sys/types.h>
	#include <fcntl.h>
	#include <sys/stat.h>
	int main(int argc, char *argv[])
	{
		int fd;
		fd = open(argv[1], O_RDWR|O_CREAT, 0666);
		if (fd == -1)
			perror("open failed");
		return 0;
	}

To avoid this, check EXT2_APPEND_FL flag first in ext2_create before really allocating new inode.

---
 fs/ext2/namei.c |   11 +++++++++++
 1 files changed, 11 insertions(+), 0 deletions(-)
fs/ext2/namei.c +11 -0
index 761fde8..b9f8998 100644
@@ -32,6 +32,7 @@
 
 #include <linux/pagemap.h>
 #include <linux/quotaops.h>
+#include <linux/namei.h>
 #include "ext2.h"
 #include "xattr.h"
 #include "acl.h"
@@ -97,6 +98,16 @@ struct dentry *ext2_get_parent(struct dentry *child)
 static int ext2_create (struct inode * dir, struct dentry * dentry, int mode, struct nameidata *nd)
 {
 	struct inode *inode;
+	int open_flag = nd->intent.open.file->f_flags;
+
+	if ((EXT2_I(dir)->i_flags & EXT2_FL_INHERITED) & EXT2_APPEND_FL) {
+		if ((open_flag & O_ACCMODE) != O_RDONLY &&
+		    !(open_flag & O_APPEND))
+			return -EPERM;
+		if (open_flag & O_TRUNC)
+			return -EPERM;
+	}
+
 
 	dquot_initialize(dir);
 
Signature · 1 line
1.7.7.1