Skip to content

Commit 0e6ebf8

Browse files
tobluxpcmoore
authored andcommitted
device_cgroup: Refactor devcgroup_seq_show to use seq_put* helpers
Replace set_access(), set_majmin(), and type_to_char() with new helpers seq_putaccess(), seq_puttype(), and seq_putversion() that write directly to 'seq_file'. Simplify devcgroup_seq_show() by hard-coding "a *:* rwm", and use the new seq_put* helper functions to list the exceptions otherwise. This allows us to remove the intermediate string buffers while maintaining the same functionality, including wildcard handling. Signed-off-by: Thorsten Blum <thorsten.blum@linux.dev> Acked-by: Serge Hallyn <serge@hallyn.com> Signed-off-by: Paul Moore <paul@paul-moore.com>
1 parent dfa024b commit 0e6ebf8

File tree

1 file changed

+25
-31
lines changed

1 file changed

+25
-31
lines changed

security/device_cgroup.c

Lines changed: 25 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -244,45 +244,40 @@ static void devcgroup_css_free(struct cgroup_subsys_state *css)
244244
#define DEVCG_DENY 2
245245
#define DEVCG_LIST 3
246246

247-
#define MAJMINLEN 13
248-
#define ACCLEN 4
249-
250-
static void set_access(char *acc, short access)
247+
static void seq_putaccess(struct seq_file *m, short access)
251248
{
252-
int idx = 0;
253-
memset(acc, 0, ACCLEN);
254249
if (access & DEVCG_ACC_READ)
255-
acc[idx++] = 'r';
250+
seq_putc(m, 'r');
256251
if (access & DEVCG_ACC_WRITE)
257-
acc[idx++] = 'w';
252+
seq_putc(m, 'w');
258253
if (access & DEVCG_ACC_MKNOD)
259-
acc[idx++] = 'm';
254+
seq_putc(m, 'm');
260255
}
261256

262-
static char type_to_char(short type)
257+
static void seq_puttype(struct seq_file *m, short type)
263258
{
264259
if (type == DEVCG_DEV_ALL)
265-
return 'a';
266-
if (type == DEVCG_DEV_CHAR)
267-
return 'c';
268-
if (type == DEVCG_DEV_BLOCK)
269-
return 'b';
270-
return 'X';
260+
seq_putc(m, 'a');
261+
else if (type == DEVCG_DEV_CHAR)
262+
seq_putc(m, 'c');
263+
else if (type == DEVCG_DEV_BLOCK)
264+
seq_putc(m, 'b');
265+
else
266+
seq_putc(m, 'X');
271267
}
272268

273-
static void set_majmin(char *str, unsigned m)
269+
static void seq_putversion(struct seq_file *m, unsigned int version)
274270
{
275-
if (m == ~0)
276-
strcpy(str, "*");
271+
if (version == ~0)
272+
seq_putc(m, '*');
277273
else
278-
sprintf(str, "%u", m);
274+
seq_printf(m, "%u", version);
279275
}
280276

281277
static int devcgroup_seq_show(struct seq_file *m, void *v)
282278
{
283279
struct dev_cgroup *devcgroup = css_to_devcgroup(seq_css(m));
284280
struct dev_exception_item *ex;
285-
char maj[MAJMINLEN], min[MAJMINLEN], acc[ACCLEN];
286281

287282
rcu_read_lock();
288283
/*
@@ -292,18 +287,17 @@ static int devcgroup_seq_show(struct seq_file *m, void *v)
292287
* This way, the file remains as a "whitelist of devices"
293288
*/
294289
if (devcgroup->behavior == DEVCG_DEFAULT_ALLOW) {
295-
set_access(acc, DEVCG_ACC_MASK);
296-
set_majmin(maj, ~0);
297-
set_majmin(min, ~0);
298-
seq_printf(m, "%c %s:%s %s\n", type_to_char(DEVCG_DEV_ALL),
299-
maj, min, acc);
290+
seq_puts(m, "a *:* rwm\n");
300291
} else {
301292
list_for_each_entry_rcu(ex, &devcgroup->exceptions, list) {
302-
set_access(acc, ex->access);
303-
set_majmin(maj, ex->major);
304-
set_majmin(min, ex->minor);
305-
seq_printf(m, "%c %s:%s %s\n", type_to_char(ex->type),
306-
maj, min, acc);
293+
seq_puttype(m, ex->type);
294+
seq_putc(m, ' ');
295+
seq_putversion(m, ex->major);
296+
seq_putc(m, ':');
297+
seq_putversion(m, ex->minor);
298+
seq_putc(m, ' ');
299+
seq_putaccess(m, ex->access);
300+
seq_putc(m, '\n');
307301
}
308302
}
309303
rcu_read_unlock();

0 commit comments

Comments
 (0)