Skip to contentSkip to conversation

device_cgroup: Replace strcpy/sprintf in set_majmin

PATCH In review Thorsten Blum 4 participants 6 messages Oct 31, 2025 – Nov 1, 2025 Security (LSM)

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 device_cgroup: Replace strcpy/sprintf in set_majmin

strcpy() is deprecated and sprintf() does not perform bounds checking either. While the current code works correctly, strscpy() and snprintf() are safer alternatives that follow secure coding best practices.

---
 security/device_cgroup.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
security/device_cgroup.c +2 -2
index dc4df7475081..a41f558f6fdd 100644
@@ -273,9 +273,9 @@ static char type_to_char(short type)
 static void set_majmin(char *str, unsigned m)
 {
 	if (m == ~0)
-		strcpy(str, "*");
+		strscpy(str, "*", MAJMINLEN);
 	else
-		sprintf(str, "%u", m);
+		snprintf(str, MAJMINLEN, "%u", m);
 }
 
 static int devcgroup_seq_show(struct seq_file *m, void *v)
Signature · 1 line
2.51.1
#1 lore raw
Quoted 16 lines · On Fri, 31 Oct 2025 12:06:47 +0100 Thorsten Blum <[email protected]>…

strcpy() is deprecated and sprintf() does not perform bounds checking either. While the current code works correctly, strscpy() and snprintf() are safer alternatives that follow secure coding best practices.

---
 security/device_cgroup.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
security/device_cgroup.c +0 -0
index dc4df7475081..a41f558f6fdd 100644
@@ -273,9 +273,9 @@ static char type_to_char(short type)
 static void set_majmin(char *str, unsigned m)

security/device_cgroup.c static char type_to_char(short type) · line 281

Quoted 10 lines
security/device_cgroup.c +2 -2
 {
 	if (m == ~0)
-		strcpy(str, "*");
+		strscpy(str, "*", MAJMINLEN);
 	else
-		sprintf(str, "%u", m);
+		snprintf(str, MAJMINLEN, "%u", m);
 }
 
 static int devcgroup_seq_show(struct seq_file *m, void *v)

There is no point using sting length limits that aren't passed into the function.

In any case the code seems to be crap (why is 'security' code always bad?) (See https://elixir.bootlin.com/linux/v6.18-rc3/source/security/device_cgroup.c#L247)
I doubt ex->major or ex->minor can ever be ~0.
So there are two sets of calls, one set passes ~0 and the other doesn't. The output buffers are then passed into another printf().

Even if ex->major can be ~0 there are much cleaner ways of writing this code.

David

#2 lore raw
Quoted 1 line · David Laight

Even if ex->major can be ~0 there are much cleaner ways of writing this code.

Thanks for pointing this out. Looking at the bigger picture makes it clear that most of the code can actually be removed. What do you think of this change?

Thanks,
Thorsten

security/device_cgroup.c +0 -0
index a41f558f6fdd..cb845b1fad6b 100644
@@ -244,7 +244,6 @@ static void devcgroup_css_free(struct cgroup_subsys_state *css)

#define DEVCG_DENY 2
#define DEVCG_LIST 3

-#define MAJMINLEN 13
#define ACCLEN 4

static void set_access(char *acc, short access) @@ -270,19 +269,11 @@ static char type_to_char(short type)

	return 'X';
}

-static void set_majmin(char *str, unsigned m)
-{
- if (m == ~0)
- strscpy(str, "*", MAJMINLEN);
- else
- snprintf(str, MAJMINLEN, "%u", m);
-}
-
static int devcgroup_seq_show(struct seq_file *m, void *v)

{
	struct dev_cgroup *devcgroup = css_to_devcgroup(seq_css(m));
	struct dev_exception_item *ex;

- char maj[MAJMINLEN], min[MAJMINLEN], acc[ACCLEN];
+ char acc[ACCLEN];

	rcu_read_lock();
	/*
@@ -293,17 +284,12 @@ static int devcgroup_seq_show(struct seq_file *m, void *v)
	 */
	if (devcgroup->behavior == DEVCG_DEFAULT_ALLOW) {
		set_access(acc, DEVCG_ACC_MASK);

- set_majmin(maj, ~0);
- set_majmin(min, ~0);
- seq_printf(m, "%c %s:%s %s\n", type_to_char(DEVCG_DEV_ALL),
- maj, min, acc);

+		seq_printf(m, "%c *:* %s\n", type_to_char(DEVCG_DEV_ALL), acc);
	} else {
		list_for_each_entry_rcu(ex, &devcgroup->exceptions, list) {
			set_access(acc, ex->access);

- set_majmin(maj, ex->major);
- set_majmin(min, ex->minor);
- seq_printf(m, "%c %s:%s %s\n", type_to_char(ex->type),
- maj, min, acc);
+ seq_printf(m, "%c %u:%u %s\n", type_to_char(ex->type),

+				   ex->major, ex->minor, acc);
		}
	}
	rcu_read_unlock();
#3 lore raw
Quoted 6 lines · On Fri, 31 Oct 2025 16:23:02 +0100 Thorsten Blum <[email protected]>…
Quoted 1 line · David Laight

Even if ex->major can be ~0 there are much cleaner ways of writing this code.

Thanks for pointing this out. Looking at the bigger picture makes it clear that most of the code can actually be removed. What do you think of this change?

That is sort of what I was thinking about, but it doesn't quite work.

Quoted 38 lines

Thanks,
Thorsten

security/device_cgroup.c +1 -10
index a41f558f6fdd..cb845b1fad6b 100644
@@ -244,7 +244,6 @@ static void devcgroup_css_free(struct cgroup_subsys_state *css)
 #define DEVCG_DENY 2
 #define DEVCG_LIST 3
 
-#define MAJMINLEN 13
 #define ACCLEN 4
 
 static void set_access(char *acc, short access)
@@ -270,19 +269,11 @@ static char type_to_char(short type)
 	return 'X';
 }
 
-static void set_majmin(char *str, unsigned m)
-{
-	if (m == ~0)
-		strscpy(str, "*", MAJMINLEN);
-	else
-		snprintf(str, MAJMINLEN, "%u", m);
-}
-
 static int devcgroup_seq_show(struct seq_file *m, void *v)
 {
 	struct dev_cgroup *devcgroup = css_to_devcgroup(seq_css(m));
 	struct dev_exception_item *ex;
-	char maj[MAJMINLEN], min[MAJMINLEN], acc[ACCLEN];
+	char acc[ACCLEN];
 
 	rcu_read_lock();
 	/*

security/device_cgroup.c static int devcgroup_seq_show(struct seq_file *m, void *v) · line 287

Quoted 8 lines
security/device_cgroup.c +1 -4
@@ -293,17 +284,12 @@ static int devcgroup_seq_show(struct seq_file *m, void *v)
 	 */
 	if (devcgroup->behavior == DEVCG_DEFAULT_ALLOW) {
 		set_access(acc, DEVCG_ACC_MASK);
-		set_majmin(maj, ~0);
-		set_majmin(min, ~0);
-		seq_printf(m, "%c %s:%s %s\n", type_to_char(DEVCG_DEV_ALL),
-			   maj, min, acc);
+		seq_printf(m, "%c *:* %s\n", type_to_char(DEVCG_DEV_ALL), acc);

type_to_char(DEVCG_DEV_ALL) is 'a' and this is the only place it happens, also acc is "rwm".
So that could be:

		seq_puts(m, "a *:* rwm\n");

security/device_cgroup.c static int devcgroup_seq_show(struct seq_file *m, void *v) · line 292

Quoted 9 lines
security/device_cgroup.c +2 -4
 	} else {
 		list_for_each_entry_rcu(ex, &devcgroup->exceptions, list) {
 			set_access(acc, ex->access);
-			set_majmin(maj, ex->major);
-			set_majmin(min, ex->minor);
-			seq_printf(m, "%c %s:%s %s\n", type_to_char(ex->type),
-				   maj, min, acc);
+			seq_printf(m, "%c %u:%u %s\n", type_to_char(ex->type),
+				   ex->major, ex->minor, acc);

It looks like both ex->major and ex->minor can be ~0.
(I'm not sure it makes any sense to have major == ~0 and minor != ~0). However this should be ok:

			seq_putc(m, type_to_char(ex->type);
			if (ex->major == ~0)
				seq_puts(m, " *:");
			else
				seq_printf(m, " %u:", ex->major);
			if (ex->minor == ~0)
				seq_puts(m, "* ");
			else
				seq_printf(m, "%u ", ex->minor);
			if (ex->access & DEVCG_ACC_READ)
				seq_putc(m, 'r');
			if (ex->access & DEVCG_ACC_WRITE)
				seq_putc(m, 'w');
			if (ex->access & DEV_ACC_MKNOD)
				seq_putc(m. 'm');
			seq_putc(m, '\n');

A less intrusive change would be to pass 'm' the the set_xxx() functions and add the separators between the calls.

David

Quoted 4 lines
security/device_cgroup.c +0 -0
 		}
 	}
 	rcu_read_unlock();
maintainer
#4 lore raw
Quoted 94 lines · David Laight
Quoted 6 lines · On Fri, 31 Oct 2025 16:23:02 +0100 Thorsten Blum <[email protected]>…
Quoted 1 line · David Laight

Even if ex->major can be ~0 there are much cleaner ways of writing this code.

Thanks for pointing this out. Looking at the bigger picture makes it clear that most of the code can actually be removed. What do you think of this change?

That is sort of what I was thinking about, but it doesn't quite work.

Quoted 38 lines

Thanks,
Thorsten

security/device_cgroup.c +1 -10
index a41f558f6fdd..cb845b1fad6b 100644
@@ -244,7 +244,6 @@ static void devcgroup_css_free(struct cgroup_subsys_state *css)
 #define DEVCG_DENY 2
 #define DEVCG_LIST 3
 
-#define MAJMINLEN 13
 #define ACCLEN 4
 
 static void set_access(char *acc, short access)
@@ -270,19 +269,11 @@ static char type_to_char(short type)
      return 'X';
 }
 
-static void set_majmin(char *str, unsigned m)
-{
-     if (m == ~0)
-             strscpy(str, "*", MAJMINLEN);
-     else
-             snprintf(str, MAJMINLEN, "%u", m);
-}
-
 static int devcgroup_seq_show(struct seq_file *m, void *v)
 {
      struct dev_cgroup *devcgroup = css_to_devcgroup(seq_css(m));
      struct dev_exception_item *ex;
-     char maj[MAJMINLEN], min[MAJMINLEN], acc[ACCLEN];
+     char acc[ACCLEN];
 
      rcu_read_lock();
      /*

security/device_cgroup.c static int devcgroup_seq_show(struct seq_file *m, void *v) · line 287

Quoted 8 lines
security/device_cgroup.c +1 -4
@@ -293,17 +284,12 @@ static int devcgroup_seq_show(struct seq_file *m, void *v)
       */
      if (devcgroup->behavior == DEVCG_DEFAULT_ALLOW) {
              set_access(acc, DEVCG_ACC_MASK);
-             set_majmin(maj, ~0);
-             set_majmin(min, ~0);
-             seq_printf(m, "%c %s:%s %s\n", type_to_char(DEVCG_DEV_ALL),
-                        maj, min, acc);
+             seq_printf(m, "%c *:* %s\n", type_to_char(DEVCG_DEV_ALL), acc);

type_to_char(DEVCG_DEV_ALL) is 'a' and this is the only place it happens, also acc is "rwm".
So that could be:

                seq_puts(m, "a *:* rwm\n");

security/device_cgroup.c static int devcgroup_seq_show(struct seq_file *m, void *v) · line 292

Quoted 9 lines
security/device_cgroup.c +2 -4
      } else {
              list_for_each_entry_rcu(ex, &devcgroup->exceptions, list) {
                      set_access(acc, ex->access);
-                     set_majmin(maj, ex->major);
-                     set_majmin(min, ex->minor);
-                     seq_printf(m, "%c %s:%s %s\n", type_to_char(ex->type),
-                                maj, min, acc);
+                     seq_printf(m, "%c %u:%u %s\n", type_to_char(ex->type),
+                                ex->major, ex->minor, acc);

It looks like both ex->major and ex->minor can be ~0.
(I'm not sure it makes any sense to have major == ~0 and minor != ~0). However this should be ok:

                        seq_putc(m, type_to_char(ex->type);
                        if (ex->major == ~0)
                                seq_puts(m, " *:");
                        else
                                seq_printf(m, " %u:", ex->major);
                        if (ex->minor == ~0)
                                seq_puts(m, "* ");
                        else
                                seq_printf(m, "%u ", ex->minor);
                        if (ex->access & DEVCG_ACC_READ)
                                seq_putc(m, 'r');
                        if (ex->access & DEVCG_ACC_WRITE)
                                seq_putc(m, 'w');
                        if (ex->access & DEV_ACC_MKNOD)
                                seq_putc(m. 'm');
                        seq_putc(m, '\n');
Quoted 2 lines

A less intrusive change would be to pass 'm' the the set_xxx() functions and add the separators between the calls.

Yes, just pass the seq_file pointer up to set_majmin() and have it do the writes/puts directly. Might as well rename if from set_majmin() to put_majmin() while you are at it.

Signature · 1 line
paul-moore.com
maintainer
#5 lore raw
In reply to Thorsten Blum
Quoted 15 lines · Thorsten Blum

strcpy() is deprecated and sprintf() does not perform bounds checking either. While the current code works correctly, strscpy() and snprintf() are safer alternatives that follow secure coding best practices.

---
 security/device_cgroup.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
security/device_cgroup.c +0 -0
index dc4df7475081..a41f558f6fdd 100644

security/device_cgroup.c static char type_to_char(short type) · line 276

Quoted 5 lines
security/device_cgroup.c +1 -1
@@ -273,9 +273,9 @@ static char type_to_char(short type)
 static void set_majmin(char *str, unsigned m)
 {
 	if (m == ~0)
-		strcpy(str, "*");
+		strscpy(str, "*", MAJMINLEN);

I dunno, I'm not saying I would say no to this, but it does look a little ridiculous. If it used some macro version which computes the length of str instead of typing in MAJMINLEN, that might be better. But we're just as likely here to get MAJMINLEN out of sync with the length of strscpy as anything else happening, and all to copy over two bytes.

security/device_cgroup.c static char type_to_char(short type) · line 278

Quoted 3 lines
security/device_cgroup.c +1 -1
 	else
-		sprintf(str, "%u", m);
+		snprintf(str, MAJMINLEN, "%u", m);

Here, to me it always looks suspect to use snprintf without checking its return value, and in this case, if snprintf cuts off the value it is in fact a potential security issue, one which did not exist before this.

So make up your mind: is it worth doing the length check or not? If not, then this switch is uncalled for. If so, then you should check the return value, else one day I may be able to whitelist a device which the admin didn't allow.

Quoted 5 lines
security/device_cgroup.c +0 -0
 }
 
 static int devcgroup_seq_show(struct seq_file *m, void *v)
Signature · 1 line
2.51.1