Skip to content

Commit d78ca3c

Browse files
Kees Cookxjamesmorris
authored andcommitted
syslog: use defined constants instead of raw numbers
Right now the syslog "type" action are just raw numbers which makes the source difficult to follow. This patch replaces the raw numbers with defined constants for some level of sanity. Signed-off-by: Kees Cook <kees.cook@canonical.com> Acked-by: John Johansen <john.johansen@canonical.com> Acked-by: Serge Hallyn <serue@us.ibm.com> Signed-off-by: James Morris <jmorris@namei.org>
1 parent 0023459 commit d78ca3c

5 files changed

Lines changed: 61 additions & 43 deletions

File tree

‎fs/proc/kmsg.c‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,28 +21,28 @@ extern wait_queue_head_t log_wait;
2121

2222
static int kmsg_open(struct inode * inode, struct file * file)
2323
{
24-
return do_syslog(1, NULL, 0, SYSLOG_FROM_FILE);
24+
return do_syslog(SYSLOG_ACTION_OPEN, NULL, 0, SYSLOG_FROM_FILE);
2525
}
2626

2727
static int kmsg_release(struct inode * inode, struct file * file)
2828
{
29-
(void) do_syslog(0, NULL, 0, SYSLOG_FROM_FILE);
29+
(void) do_syslog(SYSLOG_ACTION_CLOSE, NULL, 0, SYSLOG_FROM_FILE);
3030
return 0;
3131
}
3232

3333
static ssize_t kmsg_read(struct file *file, char __user *buf,
3434
size_t count, loff_t *ppos)
3535
{
3636
if ((file->f_flags & O_NONBLOCK) &&
37-
!do_syslog(9, NULL, 0, SYSLOG_FROM_FILE))
37+
!do_syslog(SYSLOG_ACTION_SIZE_UNREAD, NULL, 0, SYSLOG_FROM_FILE))
3838
return -EAGAIN;
39-
return do_syslog(2, buf, count, SYSLOG_FROM_FILE);
39+
return do_syslog(SYSLOG_ACTION_READ, buf, count, SYSLOG_FROM_FILE);
4040
}
4141

4242
static unsigned int kmsg_poll(struct file *file, poll_table *wait)
4343
{
4444
poll_wait(file, &log_wait, wait);
45-
if (do_syslog(9, NULL, 0, SYSLOG_FROM_FILE))
45+
if (do_syslog(SYSLOG_ACTION_SIZE_UNREAD, NULL, 0, SYSLOG_FROM_FILE))
4646
return POLLIN | POLLRDNORM;
4747
return 0;
4848
}

‎include/linux/syslog.h‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,29 @@
2121
#ifndef _LINUX_SYSLOG_H
2222
#define _LINUX_SYSLOG_H
2323

24+
/* Close the log. Currently a NOP. */
25+
#define SYSLOG_ACTION_CLOSE 0
26+
/* Open the log. Currently a NOP. */
27+
#define SYSLOG_ACTION_OPEN 1
28+
/* Read from the log. */
29+
#define SYSLOG_ACTION_READ 2
30+
/* Read all messages remaining in the ring buffer. */
31+
#define SYSLOG_ACTION_READ_ALL 3
32+
/* Read and clear all messages remaining in the ring buffer */
33+
#define SYSLOG_ACTION_READ_CLEAR 4
34+
/* Clear ring buffer. */
35+
#define SYSLOG_ACTION_CLEAR 5
36+
/* Disable printk's to console */
37+
#define SYSLOG_ACTION_CONSOLE_OFF 6
38+
/* Enable printk's to console */
39+
#define SYSLOG_ACTION_CONSOLE_ON 7
40+
/* Set level of messages printed to console */
41+
#define SYSLOG_ACTION_CONSOLE_LEVEL 8
42+
/* Return number of unread characters in the log buffer */
43+
#define SYSLOG_ACTION_SIZE_UNREAD 9
44+
/* Return size of the log buffer */
45+
#define SYSLOG_ACTION_SIZE_BUFFER 10
46+
2447
#define SYSLOG_FROM_CALL 0
2548
#define SYSLOG_FROM_FILE 1
2649

‎kernel/printk.c‎

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -259,21 +259,6 @@ static inline void boot_delay_msec(void)
259259
}
260260
#endif
261261

262-
/*
263-
* Commands to do_syslog:
264-
*
265-
* 0 -- Close the log. Currently a NOP.
266-
* 1 -- Open the log. Currently a NOP.
267-
* 2 -- Read from the log.
268-
* 3 -- Read all messages remaining in the ring buffer.
269-
* 4 -- Read and clear all messages remaining in the ring buffer
270-
* 5 -- Clear ring buffer.
271-
* 6 -- Disable printk's to console
272-
* 7 -- Enable printk's to console
273-
* 8 -- Set level of messages printed to console
274-
* 9 -- Return number of unread characters in the log buffer
275-
* 10 -- Return size of the log buffer
276-
*/
277262
int do_syslog(int type, char __user *buf, int len, bool from_file)
278263
{
279264
unsigned i, j, limit, count;
@@ -286,11 +271,11 @@ int do_syslog(int type, char __user *buf, int len, bool from_file)
286271
return error;
287272

288273
switch (type) {
289-
case 0: /* Close log */
274+
case SYSLOG_ACTION_CLOSE: /* Close log */
290275
break;
291-
case 1: /* Open log */
276+
case SYSLOG_ACTION_OPEN: /* Open log */
292277
break;
293-
case 2: /* Read from log */
278+
case SYSLOG_ACTION_READ: /* Read from log */
294279
error = -EINVAL;
295280
if (!buf || len < 0)
296281
goto out;
@@ -321,10 +306,12 @@ int do_syslog(int type, char __user *buf, int len, bool from_file)
321306
if (!error)
322307
error = i;
323308
break;
324-
case 4: /* Read/clear last kernel messages */
309+
/* Read/clear last kernel messages */
310+
case SYSLOG_ACTION_READ_CLEAR:
325311
do_clear = 1;
326312
/* FALL THRU */
327-
case 3: /* Read last kernel messages */
313+
/* Read last kernel messages */
314+
case SYSLOG_ACTION_READ_ALL:
328315
error = -EINVAL;
329316
if (!buf || len < 0)
330317
goto out;
@@ -377,21 +364,25 @@ int do_syslog(int type, char __user *buf, int len, bool from_file)
377364
}
378365
}
379366
break;
380-
case 5: /* Clear ring buffer */
367+
/* Clear ring buffer */
368+
case SYSLOG_ACTION_CLEAR:
381369
logged_chars = 0;
382370
break;
383-
case 6: /* Disable logging to console */
371+
/* Disable logging to console */
372+
case SYSLOG_ACTION_CONSOLE_OFF:
384373
if (saved_console_loglevel == -1)
385374
saved_console_loglevel = console_loglevel;
386375
console_loglevel = minimum_console_loglevel;
387376
break;
388-
case 7: /* Enable logging to console */
377+
/* Enable logging to console */
378+
case SYSLOG_ACTION_CONSOLE_ON:
389379
if (saved_console_loglevel != -1) {
390380
console_loglevel = saved_console_loglevel;
391381
saved_console_loglevel = -1;
392382
}
393383
break;
394-
case 8: /* Set level of messages printed to console */
384+
/* Set level of messages printed to console */
385+
case SYSLOG_ACTION_CONSOLE_LEVEL:
395386
error = -EINVAL;
396387
if (len < 1 || len > 8)
397388
goto out;
@@ -402,10 +393,12 @@ int do_syslog(int type, char __user *buf, int len, bool from_file)
402393
saved_console_loglevel = -1;
403394
error = 0;
404395
break;
405-
case 9: /* Number of chars in the log buffer */
396+
/* Number of chars in the log buffer */
397+
case SYSLOG_ACTION_SIZE_UNREAD:
406398
error = log_end - log_start;
407399
break;
408-
case 10: /* Size of the log buffer */
400+
/* Size of the log buffer */
401+
case SYSLOG_ACTION_SIZE_BUFFER:
409402
error = log_buf_len;
410403
break;
411404
default:

‎security/commoncap.c‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -897,9 +897,10 @@ int cap_task_prctl(int option, unsigned long arg2, unsigned long arg3,
897897
int cap_syslog(int type, bool from_file)
898898
{
899899
/* /proc/kmsg can open be opened by CAP_SYS_ADMIN */
900-
if (type != 1 && from_file)
900+
if (type != SYSLOG_ACTION_OPEN && from_file)
901901
return 0;
902-
if ((type != 3 && type != 10) && !capable(CAP_SYS_ADMIN))
902+
if ((type != SYSLOG_ACTION_READ_ALL &&
903+
type != SYSLOG_ACTION_SIZE_BUFFER) && !capable(CAP_SYS_ADMIN))
903904
return -EPERM;
904905
return 0;
905906
}

‎security/selinux/hooks.c‎

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2059,20 +2059,21 @@ static int selinux_syslog(int type, bool from_file)
20592059
return rc;
20602060

20612061
switch (type) {
2062-
case 3: /* Read last kernel messages */
2063-
case 10: /* Return size of the log buffer */
2062+
case SYSLOG_ACTION_READ_ALL: /* Read last kernel messages */
2063+
case SYSLOG_ACTION_SIZE_BUFFER: /* Return size of the log buffer */
20642064
rc = task_has_system(current, SYSTEM__SYSLOG_READ);
20652065
break;
2066-
case 6: /* Disable logging to console */
2067-
case 7: /* Enable logging to console */
2068-
case 8: /* Set level of messages printed to console */
2066+
case SYSLOG_ACTION_CONSOLE_OFF: /* Disable logging to console */
2067+
case SYSLOG_ACTION_CONSOLE_ON: /* Enable logging to console */
2068+
/* Set level of messages printed to console */
2069+
case SYSLOG_ACTION_CONSOLE_LEVEL:
20692070
rc = task_has_system(current, SYSTEM__SYSLOG_CONSOLE);
20702071
break;
2071-
case 0: /* Close log */
2072-
case 1: /* Open log */
2073-
case 2: /* Read from log */
2074-
case 4: /* Read/clear last kernel messages */
2075-
case 5: /* Clear ring buffer */
2072+
case SYSLOG_ACTION_CLOSE: /* Close log */
2073+
case SYSLOG_ACTION_OPEN: /* Open log */
2074+
case SYSLOG_ACTION_READ: /* Read from log */
2075+
case SYSLOG_ACTION_READ_CLEAR: /* Read/clear last kernel messages */
2076+
case SYSLOG_ACTION_CLEAR: /* Clear ring buffer */
20762077
default:
20772078
rc = task_has_system(current, SYSTEM__SYSLOG_MOD);
20782079
break;

0 commit comments

Comments
 (0)