Skip to content

Commit 81c09eb

Browse files
committed
libaudit: enforce kernel comm length limit
audit_log_user_comm_message accepted arbitrary-length comm strings even though comm represents /proc/self/comm and Linux TASK_COMM_LEN permits at most 15 content bytes plus the terminating NUL. Encoding an overlong value could therefore exceed the caller's stack buffer. Bound command-name inspection to the kernel limit, reject malformed caller values with EINVAL, and size the procfs and encoded-output buffers from that limit. Document the comm contract and the new validation error. Add a regression that submits an encoded 16-byte command name and verifies it is rejected with EINVAL. The exact 15-byte accepted and 16-byte rejected boundaries were also validated with AddressSanitizer.
1 parent 9b6c174 commit 81c09eb

3 files changed

Lines changed: 53 additions & 9 deletions

File tree

‎docs/audit_log_user_comm_message.3‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,31 @@ This function will log a message to the audit system using a predefined message
2222
\fIaudit_fd\fP - The fd returned by audit_open
2323
\fItype\fP - type of message, ex: AUDIT_USYS_CONFIG, AUDIT_USER_LOGIN
2424
\fImessage\fP - the message text being sent
25-
\fIcomm\fP - the program command line name, NULL if unknown
25+
\fIcomm\fP - the task command name from /proc/self/comm, NULL if unknown
2626
\fIhostname\fP - the hostname if known, NULL if unknown
2727
\fIaddr\fP - The network address of the user, NULL if unknown
2828
\fItty\fP - The tty of the user, if NULL will attempt to figure out
2929
\fIresult\fP - 1 is "success" and 0 is "failed"
3030
.fi
3131

32+
The kernel task command name is limited to 15 bytes because
33+
.B TASK_COMM_LEN
34+
is 16 bytes including the terminating null byte. A non-NULL
35+
.I comm
36+
value longer than 15 bytes is malformed and is rejected.
37+
3238
.SH "RETURN VALUE"
3339

3440
It returns the sequence number which is > 0 on success or <= 0 on error.
3541

3642
.SH "ERRORS"
3743

3844
This function returns \-1 on failure. Examine errno for more info.
45+
.TP
46+
.B EINVAL
47+
The
48+
.I comm
49+
value is longer than the kernel task command name limit of 15 bytes.
3950

4051
.SH "SEE ALSO"
4152

‎lib/audit_logging.c‎

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@
4040
#define TTY_PATH 32
4141
#define MAX_USER ((UT_NAMESIZE * 2) + 8)
4242
#define EXENAME_SIZE ((PATH_MAX * 2) + 1)
43+
/* Kernel TASK_COMM_LEN includes the terminating NUL. */
44+
#define AUDIT_COMM_LEN 16
45+
#define AUDIT_COMM_ENCODED_LEN (((AUDIT_COMM_LEN - 1) * 2) + 1)
4346

4447
// NOTE: The kernel fills in pid, uid, and loginuid of sender. Therefore,
4548
// these routines do not need to send them.
@@ -197,14 +200,17 @@ static char *_get_exename(char *exename, int size)
197200
}
198201

199202
/*
200-
* Get the command line name
201-
* NOTE: at the moment, this only escapes what the user sent
203+
* Get the command line name which the kernel's limit is 15 chars + NUL.
202204
*/
205+
static char *_get_commname(const char *comm, char *commname,
206+
unsigned int size)
207+
__attr_access ((__read_only__, 1))
208+
__attr_access ((__write_only__, 2, 3));
203209
static char *_get_commname(const char *comm, char *commname, unsigned int size)
204210
{
205211
unsigned int len;
206-
char tmp_comm[20];
207-
212+
char tmp_comm[AUDIT_COMM_LEN];
213+
208214
if (comm == NULL) {
209215
ssize_t ret;
210216
int fd = open("/proc/self/comm", O_RDONLY|O_CLOEXEC);
@@ -223,7 +229,11 @@ static char *_get_commname(const char *comm, char *commname, unsigned int size)
223229
comm = tmp_comm;
224230
}
225231

226-
len = strlen(comm);
232+
len = strnlen(comm, AUDIT_COMM_LEN);
233+
if (len == AUDIT_COMM_LEN) {
234+
errno = EINVAL;
235+
return NULL;
236+
}
227237
if (audit_value_needs_encoding(comm, len))
228238
audit_encode_value(commname, comm, len);
229239
else
@@ -367,7 +377,7 @@ int audit_log_user_message(int audit_fd, int type, const char *message,
367377
* audit_fd - The fd returned by audit_open
368378
* type - type of message, ex: AUDIT_USER, AUDIT_USYS_CONFIG, AUDIT_USER_LOGIN
369379
* message - the message being sent
370-
* comm - the program command line name
380+
* comm - the task name from /proc/self/comm, 15 bytes maximum
371381
* hostname - the hostname if known
372382
* addr - The network address of the user
373383
* tty - The tty of the user
@@ -382,7 +392,7 @@ int audit_log_user_comm_message(int audit_fd, int type, const char *message,
382392
char buf[MAX_AUDIT_MESSAGE_LENGTH];
383393
char addrbuf[INET6_ADDRSTRLEN];
384394
static char exename[EXENAME_SIZE]="";
385-
char commname[PATH_MAX*2];
395+
char commname[AUDIT_COMM_ENCODED_LEN];
386396
char ttyname[TTY_PATH];
387397
const char *success;
388398
int ret;
@@ -416,7 +426,8 @@ int audit_log_user_comm_message(int audit_fd, int type, const char *message,
416426
else if (*tty == 0)
417427
tty = NULL;
418428

419-
_get_commname(comm, commname, sizeof(commname));
429+
if (_get_commname(comm, commname, sizeof(commname)) == NULL)
430+
return -1;
420431

421432
/* Get the local name if we have a real tty */
422433
if (hostname == NULL && tty)

‎lib/test/lookup_test.c‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,27 @@ test_audit_logging_encoding(void)
481481
free(nv);
482482
}
483483

484+
/*
485+
* Test audit logging task command length validation.
486+
* Input variables: none. Return codes: none, aborts on failure.
487+
*/
488+
static void
489+
test_audit_logging_comm_length(void)
490+
{
491+
/* Sixteen content bytes exceed kernel TASK_COMM_LEN by one. */
492+
char comm[17];
493+
int rc;
494+
495+
memset(comm, 'a', sizeof(comm) - 1);
496+
comm[0] = '"';
497+
comm[sizeof(comm) - 1] = '\0';
498+
errno = 0;
499+
rc = audit_log_user_comm_message(0, AUDIT_USER, "test", comm,
500+
NULL, NULL, "", 1);
501+
assert(rc == -1);
502+
assert(errno == EINVAL);
503+
}
504+
484505
int
485506
main(void)
486507
{
@@ -515,5 +536,6 @@ main(void)
515536
test_msg_typetab();
516537
test_optab();
517538
test_audit_logging_encoding();
539+
test_audit_logging_comm_length();
518540
return EXIT_SUCCESS;
519541
}

0 commit comments

Comments
 (0)