Skip to contentSkip to conversation

oprofilefs: Handle zero-length writes.

PATCH In review Mike Waychison 2 participants 2 messages Aug 12, 2011 – Aug 17, 2011

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 oprofilefs: Handle zero-length writes.

Currently in oprofilefs, files that use ulong_fops mis-handle writes of zero length. A count of 0 causes oprofilefs_ulong_from_user to return 0 (success), which then leads to oprofile_set_ulong being called to stuff "value" into file->private_data without it being initialized.

Fix this by moving the check for a zero-length write up into ulong_write_file.

---
 drivers/oprofile/oprofilefs.c |    5 ++---
 1 files changed, 2 insertions(+), 3 deletions(-)
drivers/oprofile/oprofilefs.c +2 -3
index e9ff6f7..ee14e6e 100644
@@ -65,9 +65,6 @@ int oprofilefs_ulong_from_user(unsigned long *val, char const __user *buf, size_
 	char tmpbuf[TMPBUFSIZE];
 	unsigned long flags;
 
-	if (!count)
-		return 0;
-
 	if (count > TMPBUFSIZE - 1)
 		return -EINVAL;
 
@@ -97,6 +94,8 @@ static ssize_t ulong_write_file(struct file *file, char const __user *buf, size_
 
 	if (*offset)
 		return -EINVAL;
+	if (count == 0)
+		return 0;
 
 	retval = oprofilefs_ulong_from_user(&value, buf, count);
 	if (retval)
Signature · 1 line
1.7.3.1
#1 lore raw
Quoted 18 lines · Mike Waychison

Currently in oprofilefs, files that use ulong_fops mis-handle writes of zero length. A count of 0 causes oprofilefs_ulong_from_user to return 0 (success), which then leads to oprofile_set_ulong being called to stuff "value" into file->private_data without it being initialized.

Fix this by moving the check for a zero-length write up into ulong_write_file.

---
 drivers/oprofile/oprofilefs.c |    5 ++---
 1 files changed, 2 insertions(+), 3 deletions(-)
drivers/oprofile/oprofilefs.c +0 -0
index e9ff6f7..ee14e6e 100644

drivers/oprofile/oprofilefs.c int oprofilefs_ulong_from_user(unsigned long *val, char const __user *buf, size_ · line 70

Quoted 6 lines
drivers/oprofile/oprofilefs.c +0 -3
@@ -65,9 +65,6 @@ int oprofilefs_ulong_from_user(unsigned long *val, char const __user *buf, size_
 	char tmpbuf[TMPBUFSIZE];
 	unsigned long flags;
 
-	if (!count)
-		return 0;
-

Yes, *val is clearly used uninitialized for !count.

But it might be ok, not to touch it in oprofilefs_ulong_from_user.

Quoted 1 line

From man 3 write:

"if nbyte is zero and the file is a regular file ... the write() function shall return zero and have no other results"

Actually, oprofilefs_ulong_from_user() must be called with an initialized value ...

drivers/oprofile/oprofilefs.c static ssize_t ulong_write_file(struct file *file, char const __user *buf, size_ · line 98

Quoted 9 lines
drivers/oprofile/oprofilefs.c +2 -0
 	if (count > TMPBUFSIZE - 1)
 		return -EINVAL;
@@ -97,6 +94,8 @@ static ssize_t ulong_write_file(struct file *file, char const __user *buf, size_
 
 	if (*offset)
 		return -EINVAL;
+	if (count == 0)
+		return 0;

... or we add this check to all other users of oprofilefs_ulong_from_user() too. Without those checks they would set its value to 0 if count is 0.

A small nitpick: I would prefer

	if (!count) ...

-Robert

Quoted 7 lines
drivers/oprofile/oprofilefs.c +0 -0
 	retval = oprofilefs_ulong_from_user(&value, buf, count);
 	if (retval)
Signature · 1 line
1.7.3.1
Signature · 2 lines
Advanced Micro Devices, Inc.
Operating System Research Center