Skip to contentSkip to conversation

proc: use strnlen() for name validation in __proc_create

PATCH Applied Thorsten Blum 3 participants 4 messages Apr 21

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 proc: use strnlen() for name validation in __proc_create

Replace strlen(fn) with strnlen(fn, NAME_MAX + 1) when validating the final path component in __proc_create().

This preserves the existing name limit while bounding the length scan to one byte past the maximum name length. Handle empty names separately, and treat names longer than NAME_MAX as too long.

---
 fs/proc/generic.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
fs/proc/generic.c +7 -3
index 8bb81e58c9d8..3063080f3bb2 100644
@@ -427,9 +427,13 @@ static struct proc_dir_entry *__proc_create(struct proc_dir_entry **parent,
 	if (xlate_proc_name(name, parent, &fn) != 0)
 		goto out;
 	qstr.name = fn;
-	qstr.len = strlen(fn);
-	if (qstr.len == 0 || qstr.len >= 256) {
-		WARN(1, "name len %u\n", qstr.len);
+	qstr.len = strnlen(fn, NAME_MAX + 1);
+	if (qstr.len == 0) {
+		WARN(1, "empty name\n");
+		return NULL;
+	}
+	if (qstr.len > NAME_MAX) {
+		WARN(1, "name too long\n");
 		return NULL;
 	}
 	if (qstr.len == 1 && fn[0] == '.') {
reviewer Reviewed-by on v1 1/1
#1 lore raw
Quoted 8 lines · Thorsten Blum

Replace strlen(fn) with strnlen(fn, NAME_MAX + 1) when validating the final path component in __proc_create().

This preserves the existing name limit while bounding the length scan to one byte past the maximum name length. Handle empty names separately, and treat names longer than NAME_MAX as too long.

Looks good. Feel free to add:

Honza

Quoted 25 lines
---
 fs/proc/generic.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
fs/proc/generic.c +7 -3
index 8bb81e58c9d8..3063080f3bb2 100644
@@ -427,9 +427,13 @@ static struct proc_dir_entry *__proc_create(struct proc_dir_entry **parent,
 	if (xlate_proc_name(name, parent, &fn) != 0)
 		goto out;
 	qstr.name = fn;
-	qstr.len = strlen(fn);
-	if (qstr.len == 0 || qstr.len >= 256) {
-		WARN(1, "name len %u\n", qstr.len);
+	qstr.len = strnlen(fn, NAME_MAX + 1);
+	if (qstr.len == 0) {
+		WARN(1, "empty name\n");
+		return NULL;
+	}
+	if (qstr.len > NAME_MAX) {
+		WARN(1, "name too long\n");
 		return NULL;
 	}
 	if (qstr.len == 1 && fn[0] == '.') {
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
#2 lore raw
In reply to Thorsten Blum
Quoted 6 lines · Thorsten Blum

Replace strlen(fn) with strnlen(fn, NAME_MAX + 1) when validating the final path component in __proc_create().

This preserves the existing name limit while bounding the length scan to one byte past the maximum name length. Handle empty names separately, and treat names longer than NAME_MAX as too long.

256 in the code is really U8_MAX+1 (see proc_dir_entry.namelen).

The fact that NAME_MAX is also 255 is a coincidence. I didn't thought about NAME_MAX when writing this code.

Can you just change 256 to NAME_MAX?

/proc/alexey

Quoted 16 lines
fs/proc/generic.c +7 -3
@@ -427,9 +427,13 @@ static struct proc_dir_entry *__proc_create(struct proc_dir_entry **parent,
 	if (xlate_proc_name(name, parent, &fn) != 0)
 		goto out;
 	qstr.name = fn;
-	qstr.len = strlen(fn);
-	if (qstr.len == 0 || qstr.len >= 256) {
-		WARN(1, "name len %u\n", qstr.len);
+	qstr.len = strnlen(fn, NAME_MAX + 1);
+	if (qstr.len == 0) {
+		WARN(1, "empty name\n");
+		return NULL;
+	}
+	if (qstr.len > NAME_MAX) {
+		WARN(1, "name too long\n");
#3 lore raw
Quoted 8 lines · Alexey Dobriyan
Quoted 6 lines · Thorsten Blum

Replace strlen(fn) with strnlen(fn, NAME_MAX + 1) when validating the final path component in __proc_create().

This preserves the existing name limit while bounding the length scan to one byte past the maximum name length. Handle empty names separately, and treat names longer than NAME_MAX as too long.

Quoted 6 lines

256 in the code is really U8_MAX+1 (see proc_dir_entry.namelen).

The fact that NAME_MAX is also 255 is a coincidence. I didn't thought about NAME_MAX when writing this code.

Can you just change 256 to NAME_MAX?

I assume you meant U8_MAX here ^^

I don't mind switching to U8_MAX, but NAME_MAX seems semantically better since we're validating a filename.

Thanks,
Thorsten