linux-fsdevel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 0/4] [RESEND 2] FMODE_NONOTIFY and FMODE_NEG_OFFSET bits
@ 2010-02-07  3:28 Wu Fengguang
  2010-02-07  3:28 ` [PATCH 1/4] fanotify: fix FMODE_NONOTIFY bit number Wu Fengguang
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: Wu Fengguang @ 2010-02-07  3:28 UTC (permalink / raw)
  To: Andrew Morton
  Cc: Al Viro, Roland Dreier, H. Peter Anvin, Borislav Petkov,
	Jamie Lokier, Eric Paris, Peter Zijlstra, linux-fsdevel,
	Wu Fengguang, LKML

Andrew,

Changelog

	- PATCH 1/4 is retained: I'm not sure if it has been merged into Eric's tree
	- PATCH 2/4 is changed to use Peter's HWEIGHT32()

Patches

	O_* and FMODE_NONOTIFY collision fix/check
		[PATCH 1/4] fanotify: fix FMODE_NONOTIFY bit number
		[PATCH 2/4] vfs: O_* bit numbers uniqueness check

	allow negative f_pos for /dev/kmem
		[PATCH 3/4] vfs: introduce FMODE_NEG_OFFSET for allowing negative f_pos
		[PATCH 4/4] devmem: dont allow seek to last page

Thanks,
Fengguang

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH 1/4] fanotify: fix FMODE_NONOTIFY bit number
  2010-02-07  3:28 [PATCH 0/4] [RESEND 2] FMODE_NONOTIFY and FMODE_NEG_OFFSET bits Wu Fengguang
@ 2010-02-07  3:28 ` Wu Fengguang
  2010-02-07  3:28 ` [PATCH 2/4] vfs: O_* bit numbers uniqueness check Wu Fengguang
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: Wu Fengguang @ 2010-02-07  3:28 UTC (permalink / raw)
  To: Andrew Morton
  Cc: Al Viro, Eric Paris, Wu Fengguang, Roland Dreier, H. Peter Anvin,
	Borislav Petkov, Jamie Lokier, Peter Zijlstra, linux-fsdevel,
	LKML

[-- Attachment #1: fanotify-bit-fix --]
[-- Type: text/plain, Size: 1170 bytes --]

FMODE_NONOTIFY=0x800000 collides with __O_SYNC in sparc,
so change it to 0x1000000.

CC: Eric Paris <eparis@redhat.com>
Signed-off-by: Wu Fengguang <fengguang.wu@intel.com>
---
 include/asm-generic/fcntl.h |    2 +-
 include/linux/fs.h          |    2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

--- linux.orig/include/asm-generic/fcntl.h	2010-01-05 10:42:36.000000000 +0800
+++ linux/include/asm-generic/fcntl.h	2010-01-05 10:42:57.000000000 +0800
@@ -5,7 +5,7 @@
 
 /*
  * FMODE_EXEC is 0x20
- * FMODE_NONOTIFY is 0x800000
+ * FMODE_NONOTIFY is 0x1000000
  * These cannot be used by userspace O_* until internal and external open
  * flags are split.
  * -Eric Paris
--- linux.orig/include/linux/fs.h	2010-01-05 10:40:33.000000000 +0800
+++ linux/include/linux/fs.h	2010-01-05 10:42:07.000000000 +0800
@@ -88,7 +88,7 @@ struct inodes_stat_t {
 #define FMODE_NOCMTIME		((__force fmode_t)2048)
 
 /* File was opened by fanotify and shouldn't generate fanotify events */
-#define FMODE_NONOTIFY		((__force fmode_t)8388608)
+#define FMODE_NONOTIFY		((__force fmode_t)0x1000000)
 
 /*
  * The below are the various read and write types that we support. Some of



^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH 2/4] vfs: O_* bit numbers uniqueness check
  2010-02-07  3:28 [PATCH 0/4] [RESEND 2] FMODE_NONOTIFY and FMODE_NEG_OFFSET bits Wu Fengguang
  2010-02-07  3:28 ` [PATCH 1/4] fanotify: fix FMODE_NONOTIFY bit number Wu Fengguang
@ 2010-02-07  3:28 ` Wu Fengguang
  2010-02-07  3:28 ` [PATCH 3/4] vfs: introduce FMODE_NEG_OFFSET for allowing negative f_pos Wu Fengguang
  2010-02-07  3:28 ` [PATCH 4/4] devmem: dont allow seek to last page Wu Fengguang
  3 siblings, 0 replies; 5+ messages in thread
From: Wu Fengguang @ 2010-02-07  3:28 UTC (permalink / raw)
  To: Andrew Morton
  Cc: Al Viro, David Miller, Stephen Rothwell, Al Viro,
	Christoph Hellwig, Eric Paris, Roland Dreier, Jamie Lokier,
	Andreas Schwab, Wu Fengguang, H. Peter Anvin, Borislav Petkov,
	Peter Zijlstra, linux-fsdevel, LKML

[-- Attachment #1: fcntl-bit-check.patch --]
[-- Type: text/plain, Size: 2120 bytes --]

The O_* bit numbers are defined in 20+ arch/*, and can silently overlap.
Add a compile time check to ensure the uniqueness as suggested by David
Miller.

v5: use Peter's HWEIGHT32()
v4: use the nice hweight_long() (suggested by Jamie)
    split O_SYNC to (__O_SYNC | O_DSYNC) (suggested by Andreas)
    take away the FMODE_* and O_RANDOM bits
v3: change to BUILD_BUG_ON() (suggested by Roland)

CC: David Miller <davem@davemloft.net>
CC: Stephen Rothwell <sfr@canb.auug.org.au>
CC: Al Viro <viro@zeniv.linux.org.uk>
CC: Christoph Hellwig <hch@infradead.org>
CC: Eric Paris <eparis@redhat.com>
CC: Roland Dreier <rdreier@cisco.com>
CC: Jamie Lokier <jamie@shareable.org>
CC: Andreas Schwab <schwab@linux-m68k.org>
Signed-off-by: Wu Fengguang <fengguang.wu@intel.com>
---
 fs/fcntl.c                  |   14 ++++++++++++--
 include/asm-generic/fcntl.h |    2 ++
 2 files changed, 14 insertions(+), 2 deletions(-)

--- linux.orig/fs/fcntl.c	2010-01-30 18:09:33.000000000 +0800
+++ linux/fs/fcntl.c	2010-02-07 11:17:36.000000000 +0800
@@ -741,11 +741,21 @@ void kill_fasync(struct fasync_struct **
 }
 EXPORT_SYMBOL(kill_fasync);
 
-static int __init fasync_init(void)
+static int __init fcntl_init(void)
 {
+	/* please add new bits here to ensure allocation uniqueness */
+	BUILD_BUG_ON(17 != HWEIGHT32(
+		O_RDONLY	| O_WRONLY	| O_RDWR	|
+		O_CREAT		| O_EXCL	| O_NOCTTY	|
+		O_TRUNC		| O_APPEND	| O_NONBLOCK	|
+		__O_SYNC	| O_DSYNC	| FASYNC	|
+		O_DIRECT	| O_LARGEFILE	| O_DIRECTORY	|
+		O_NOFOLLOW	| O_NOATIME	| O_CLOEXEC
+		));
+
 	fasync_cache = kmem_cache_create("fasync_cache",
 		sizeof(struct fasync_struct), 0, SLAB_PANIC, NULL);
 	return 0;
 }
 
-module_init(fasync_init)
+module_init(fcntl_init)
--- linux.orig/include/asm-generic/fcntl.h	2010-02-07 11:17:04.000000000 +0800
+++ linux/include/asm-generic/fcntl.h	2010-02-07 11:17:06.000000000 +0800
@@ -4,6 +4,8 @@
 #include <linux/types.h>
 
 /*
+ * When introducing new O_* bits, please check its uniqueness in fcntl_init().
+ *
  * FMODE_EXEC is 0x20
  * FMODE_NONOTIFY is 0x1000000
  * These cannot be used by userspace O_* until internal and external open



^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH 3/4] vfs: introduce FMODE_NEG_OFFSET for allowing negative f_pos
  2010-02-07  3:28 [PATCH 0/4] [RESEND 2] FMODE_NONOTIFY and FMODE_NEG_OFFSET bits Wu Fengguang
  2010-02-07  3:28 ` [PATCH 1/4] fanotify: fix FMODE_NONOTIFY bit number Wu Fengguang
  2010-02-07  3:28 ` [PATCH 2/4] vfs: O_* bit numbers uniqueness check Wu Fengguang
@ 2010-02-07  3:28 ` Wu Fengguang
  2010-02-07  3:28 ` [PATCH 4/4] devmem: dont allow seek to last page Wu Fengguang
  3 siblings, 0 replies; 5+ messages in thread
From: Wu Fengguang @ 2010-02-07  3:28 UTC (permalink / raw)
  To: Andrew Morton
  Cc: Al Viro, Heiko Carstens, Wu Fengguang, KAMEZAWA Hiroyuki,
	Roland Dreier, H. Peter Anvin, Borislav Petkov, Jamie Lokier,
	Eric Paris, Peter Zijlstra, linux-fsdevel, LKML

[-- Attachment #1: f_pos-fix --]
[-- Type: text/plain, Size: 3702 bytes --]

From: KAMEZAWA Hiroyuki <kamezawa.hiroyu@jp.fujitsu.com>

Now, rw_verify_area() checsk f_pos is negative or not. And if
negative, returns -EINVAL.

But, some special files as /dev/(k)mem and /proc/<pid>/mem etc..
has negative offsets. And we can't do any access via read/write
to the file(device).

So introduce FMODE_NEG_OFFSET to allow negative file offsets.

Changelog: v5->v6
 - use FMODE_NEG_OFFSET (suggested by Al)
 - rebased onto 2.6.33-rc1

Changelog: v4->v5
 - clean up patches dor /dev/mem.
 - rebased onto 2.6.32-rc1

Changelog: v3->v4
 - make changes in mem.c aligned.
 - change __negative_fpos_check() to return int. 
 - fixed bug in "pos" check.
 - added comments.

Changelog: v2->v3
 - fixed bug in rw_verify_area (it cannot be compiled)

CC: Al Viro <viro@ZenIV.linux.org.uk>
CC: Heiko Carstens <heiko.carstens@de.ibm.com>
Signed-off-by: Wu Fengguang <fengguang.wu@intel.com>
Signed-off-by: KAMEZAWA Hiroyuki <kamezawa.hiroyu@jp.fujitsu.com>
---
 drivers/char/mem.c |    4 ++++
 fs/proc/base.c     |    2 ++
 fs/read_write.c    |   21 +++++++++++++++++++--
 include/linux/fs.h |    3 +++
 4 files changed, 28 insertions(+), 2 deletions(-)

--- linux.orig/fs/read_write.c	2010-02-02 21:58:08.000000000 +0800
+++ linux/fs/read_write.c	2010-02-07 11:18:07.000000000 +0800
@@ -205,6 +205,20 @@ bad:
 }
 #endif
 
+static int
+__negative_fpos_check(struct file *file, loff_t pos, size_t count)
+{
+	/*
+	 * pos or pos+count is negative here, check overflow.
+	 * too big "count" will be caught in rw_verify_area().
+	 */
+	if ((pos < 0) && (pos + count < pos))
+		return -EOVERFLOW;
+	if (file->f_mode & FMODE_NEG_OFFSET)
+		return 0;
+	return -EINVAL;
+}
+
 /*
  * rw_verify_area doesn't like huge counts. We limit
  * them to something that fits in "int" so that others
@@ -222,8 +236,11 @@ int rw_verify_area(int read_write, struc
 	if (unlikely((ssize_t) count < 0))
 		return retval;
 	pos = *ppos;
-	if (unlikely((pos < 0) || (loff_t) (pos + count) < 0))
-		return retval;
+	if (unlikely((pos < 0) || (loff_t) (pos + count) < 0)) {
+		retval = __negative_fpos_check(file, pos, count);
+		if (retval)
+			return retval;
+	}
 
 	if (unlikely(inode->i_flock && mandatory_lock(inode))) {
 		retval = locks_mandatory_area(
--- linux.orig/include/linux/fs.h	2010-02-07 11:17:04.000000000 +0800
+++ linux/include/linux/fs.h	2010-02-07 11:18:07.000000000 +0800
@@ -90,6 +90,9 @@ struct inodes_stat_t {
 /* File was opened by fanotify and shouldn't generate fanotify events */
 #define FMODE_NONOTIFY		((__force fmode_t)0x1000000)
 
+/* File is huge (eg. /dev/kmem): treat loff_t as unsigned */
+#define FMODE_NEG_OFFSET	((__force fmode_t)0x2000)
+
 /*
  * The below are the various read and write types that we support. Some of
  * them include behavioral modifiers that send information down to the
--- linux.orig/drivers/char/mem.c	2010-01-30 17:39:15.000000000 +0800
+++ linux/drivers/char/mem.c	2010-02-07 11:18:07.000000000 +0800
@@ -861,6 +861,10 @@ static int memory_open(struct inode *ino
 	if (dev->dev_info)
 		filp->f_mapping->backing_dev_info = dev->dev_info;
 
+	/* Is /dev/mem or /dev/kmem ? */
+	if (dev->dev_info == &directly_mappable_cdev_bdi)
+		filp->f_mode |= FMODE_NEG_OFFSET;
+
 	if (dev->fops->open)
 		return dev->fops->open(inode, filp);
 
--- linux.orig/fs/proc/base.c	2010-01-30 17:39:15.000000000 +0800
+++ linux/fs/proc/base.c	2010-02-07 11:18:08.000000000 +0800
@@ -861,6 +861,8 @@ static const struct file_operations proc
 static int mem_open(struct inode* inode, struct file* file)
 {
 	file->private_data = (void*)((long)current->self_exec_id);
+	/* OK to pass negative loff_t, we can catch out-of-range */
+	file->f_mode |= FMODE_NEG_OFFSET;
 	return 0;
 }
 

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH 4/4] devmem: dont allow seek to last page
  2010-02-07  3:28 [PATCH 0/4] [RESEND 2] FMODE_NONOTIFY and FMODE_NEG_OFFSET bits Wu Fengguang
                   ` (2 preceding siblings ...)
  2010-02-07  3:28 ` [PATCH 3/4] vfs: introduce FMODE_NEG_OFFSET for allowing negative f_pos Wu Fengguang
@ 2010-02-07  3:28 ` Wu Fengguang
  3 siblings, 0 replies; 5+ messages in thread
From: Wu Fengguang @ 2010-02-07  3:28 UTC (permalink / raw)
  To: Andrew Morton
  Cc: Al Viro, OGAWA Hirofumi, Wu Fengguang, Roland Dreier,
	H. Peter Anvin, Borislav Petkov, Jamie Lokier, Eric Paris,
	Peter Zijlstra, linux-fsdevel, LKML

[-- Attachment #1: mem-seek-fix --]
[-- Type: text/plain, Size: 1374 bytes --]

So as to return a uniform error -EOVERFLOW instead of a random one:

# kmem-seek 0xfffffffffffffff0
seek /dev/kmem: Device or resource busy
# kmem-seek 0xfffffffffffffff1
seek /dev/kmem: Block device required

Suggested by OGAWA Hirofumi.

CC: OGAWA Hirofumi <hirofumi@mail.parknet.co.jp>
Reviewed-by: KAMEZAWA Hiroyuki <kamezawa.hiroyu@jp.fujitsu.com>
Signed-off-by: Wu Fengguang <fengguang.wu@intel.com>
---
 drivers/char/mem.c |   19 +++++++++++++------
 1 file changed, 13 insertions(+), 6 deletions(-)

--- linux.orig/drivers/char/mem.c	2010-01-15 09:11:25.000000000 +0800
+++ linux/drivers/char/mem.c	2010-01-20 09:45:55.000000000 +0800
@@ -700,16 +700,23 @@ static loff_t memory_lseek(struct file *
 
 	mutex_lock(&file->f_path.dentry->d_inode->i_mutex);
 	switch (orig) {
-		case 0:
+		case SEEK_CUR:
+			offset += file->f_pos;
+			if ((unsigned long long)offset <
+			    (unsigned long long)file->f_pos) {
+				ret = -EOVERFLOW;
+				break;
+			}
+		case SEEK_SET:
+			/* to avoid userland mistaking f_pos=-9 as -EBADF=-9 */
+			if ((unsigned long long)offset >= ~0xFFFULL) {
+				ret = -EOVERFLOW;
+				break;
+			}
 			file->f_pos = offset;
 			ret = file->f_pos;
 			force_successful_syscall_return();
 			break;
-		case 1:
-			file->f_pos += offset;
-			ret = file->f_pos;
-			force_successful_syscall_return();
-			break;
 		default:
 			ret = -EINVAL;
 	}



^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2010-02-07  3:44 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-02-07  3:28 [PATCH 0/4] [RESEND 2] FMODE_NONOTIFY and FMODE_NEG_OFFSET bits Wu Fengguang
2010-02-07  3:28 ` [PATCH 1/4] fanotify: fix FMODE_NONOTIFY bit number Wu Fengguang
2010-02-07  3:28 ` [PATCH 2/4] vfs: O_* bit numbers uniqueness check Wu Fengguang
2010-02-07  3:28 ` [PATCH 3/4] vfs: introduce FMODE_NEG_OFFSET for allowing negative f_pos Wu Fengguang
2010-02-07  3:28 ` [PATCH 4/4] devmem: dont allow seek to last page Wu Fengguang

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).