Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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>

authored by

Thorsten Blum and committed by
Paul Moore
0e6ebf87 dfa024bc

+25 -31
+25 -31
security/device_cgroup.c
··· 244 244 #define DEVCG_DENY 2 245 245 #define DEVCG_LIST 3 246 246 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) 251 248 { 252 - int idx = 0; 253 - memset(acc, 0, ACCLEN); 254 249 if (access & DEVCG_ACC_READ) 255 - acc[idx++] = 'r'; 250 + seq_putc(m, 'r'); 256 251 if (access & DEVCG_ACC_WRITE) 257 - acc[idx++] = 'w'; 252 + seq_putc(m, 'w'); 258 253 if (access & DEVCG_ACC_MKNOD) 259 - acc[idx++] = 'm'; 254 + seq_putc(m, 'm'); 260 255 } 261 256 262 - static char type_to_char(short type) 257 + static void seq_puttype(struct seq_file *m, short type) 263 258 { 264 259 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'); 271 267 } 272 268 273 - static void set_majmin(char *str, unsigned m) 269 + static void seq_putversion(struct seq_file *m, unsigned int version) 274 270 { 275 - if (m == ~0) 276 - strcpy(str, "*"); 271 + if (version == ~0) 272 + seq_putc(m, '*'); 277 273 else 278 - sprintf(str, "%u", m); 274 + seq_printf(m, "%u", version); 279 275 } 280 276 281 277 static int devcgroup_seq_show(struct seq_file *m, void *v) 282 278 { 283 279 struct dev_cgroup *devcgroup = css_to_devcgroup(seq_css(m)); 284 280 struct dev_exception_item *ex; 285 - char maj[MAJMINLEN], min[MAJMINLEN], acc[ACCLEN]; 286 281 287 282 rcu_read_lock(); 288 283 /* ··· 287 292 * This way, the file remains as a "whitelist of devices" 288 293 */ 289 294 if (devcgroup->behavior == DEVCG_DEFAULT_ALLOW) { 290 - set_access(acc, DEVCG_ACC_MASK); 291 - set_majmin(maj, ~0); 292 - set_majmin(min, ~0); 293 - seq_printf(m, "%c %s:%s %s\n", type_to_char(DEVCG_DEV_ALL), 294 - maj, min, acc); 295 + seq_puts(m, "a *:* rwm\n"); 295 296 } else { 296 297 list_for_each_entry_rcu(ex, &devcgroup->exceptions, list) { 297 - set_access(acc, ex->access); 298 - set_majmin(maj, ex->major); 299 - set_majmin(min, ex->minor); 300 - seq_printf(m, "%c %s:%s %s\n", type_to_char(ex->type), 301 - maj, min, acc); 298 + seq_puttype(m, ex->type); 299 + seq_putc(m, ' '); 300 + seq_putversion(m, ex->major); 301 + seq_putc(m, ':'); 302 + seq_putversion(m, ex->minor); 303 + seq_putc(m, ' '); 304 + seq_putaccess(m, ex->access); 305 + seq_putc(m, '\n'); 302 306 } 303 307 } 304 308 rcu_read_unlock();