[PATCH 2/2] debugfs: Pre-allocate u32 array to thwart a race.

From: Konrad Rzeszutek Wilk
Date: Wed Sep 19 2012 - 14:04:22 EST


Linus spotted via code inspection "bug is slightly subtler
and probably harder to hit (but also harder to fix):

The whole format_array_alloc() code is one buggy piece of sh*t,
since afaik there is nothing that guarantees that the values cannot
change. So the notion of "let's format the output once to know how big
it is, and then a second time to actually print things into the array
we just allocated based on the first time" is pure and utter garbage,
afaik."

This patch fixes this by pre-allocating the buffer to the maximum
size during debugfs initialization by the driver. We print %u values,
so the math is pretty straightforward: 10 bytes for the maximum
that %u can use (4294967295) + spaces for the number of elements
and \n\0. We also add an extra byte to compensate for the
data->len == size check which we would hit if all of the array
entries were of their maximum size (-1U).

If we end up using exactly up to the size we allocated (this includes
the extra byte), then we allocate a new buffer twice the size.
And if we fail again, we print a warning as snprintf is doing
something silly.

Reported-by: Linus Torvalds <torvalds@xxxxxxxxxxxxxxxxxxxx>
Signed-off-by: Konrad Rzeszutek Wilk <konrad.wilk@xxxxxxxxxx>
---
fs/debugfs/file.c | 66 ++++++++++++++++++++++------------------------------
1 files changed, 28 insertions(+), 38 deletions(-)

diff --git a/fs/debugfs/file.c b/fs/debugfs/file.c
index c6d9088..4c59200 100644
--- a/fs/debugfs/file.c
+++ b/fs/debugfs/file.c
@@ -524,12 +524,13 @@ EXPORT_SYMBOL_GPL(debugfs_create_blob);
struct array_data {
void *array;
u32 elements;
+ ssize_t len;
+ char *buf;
struct mutex lock;
};

static int u32_array_open(struct inode *inode, struct file *file)
{
- file->private_data = NULL;
return nonseekable_open(inode, file);
}

@@ -560,20 +561,6 @@ static size_t format_array(char *buf, size_t bufsize, const char *fmt,
return ret;
}

-static char *format_array_alloc(const char *fmt, u32 *array,
- u32 array_size)
-{
- size_t len = format_array(NULL, 0, fmt, array, array_size);
- char *ret;
-
- ret = kmalloc(len, GFP_KERNEL);
- if (ret == NULL)
- return NULL;
-
- format_array(ret, len, fmt, array, array_size);
- return ret;
-}
-
static ssize_t u32_array_read(struct file *file, char __user *buf, size_t len,
loff_t *ppos)
{
@@ -582,37 +569,33 @@ static ssize_t u32_array_read(struct file *file, char __user *buf, size_t len,
size_t size;

mutex_lock(&data->lock);
- if (*ppos == 0) {
- if (file->private_data) {
- kfree(file->private_data);
- file->private_data = NULL;
- }
-
- file->private_data = format_array_alloc("%u", data->array,
- data->elements);
+ if (*ppos == 0)
+ size = format_array(data->buf, data->len - 1 /* for \0 */,
+ "%u", data->array, data->elements);
+ else
+ size = strlen(data->buf);
+
+ if (size == data->len) {
+ char *p = krealloc(data->buf, data->len * 2, GFP_KERNEL);
+ if (ZERO_OR_NULL_PTR(p))
+ goto out;
+ data->buf = p;
+ data->len *= 2;
+ size = format_array(data->buf, data->len - 1 /* for \0 */,
+ "%u", data->array, data->elements);
+ /* It keeps on growing! Early pre-allocation MUST be wrong. */
+ WARN_ON(size == data->len);
}
-
- size = 0;
- if (file->private_data)
- size = strlen(file->private_data);
-
+out:
size = simple_read_from_buffer(buf, len, ppos,
- file->private_data, size);
+ data->buf, size);
mutex_unlock(&data->lock);
return size;
}

-static int u32_array_release(struct inode *inode, struct file *file)
-{
- kfree(file->private_data);
-
- return 0;
-}
-
static const struct file_operations u32_array_fops = {
.owner = THIS_MODULE,
.open = u32_array_open,
- .release = u32_array_release,
.read = u32_array_read,
.llseek = no_llseek,
};
@@ -648,7 +631,14 @@ struct dentry *debugfs_create_u32_array(const char *name, umode_t mode,
data->array = array;
data->elements = elements;
mutex_init(&data->lock);
-
+ data->len = elements * 10 /* max value for %u */ +
+ elements - 1 /* spaces */ + 2 /* \n\0 */ +
+ 1 /* to thwart the size == data->len check. */;
+ data->buf = kmalloc(data->len, GFP_KERNEL);
+ if (!data->buf) {
+ kfree(data);
+ return ERR_PTR(-ENOMEM);
+ }
return debugfs_create_file(name, mode, parent, data, &u32_array_fops);
}
EXPORT_SYMBOL_GPL(debugfs_create_u32_array);
--
1.7.7.6

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/