Skip to content

Commit f045e9d

Browse files
committed
prevent exe corruption on last byte if max length
1 parent 528b119 commit f045e9d

1 file changed

Lines changed: 15 additions & 8 deletions

File tree

lib/audit_logging.c

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939

4040
#define TTY_PATH 32
4141
#define MAX_USER ((UT_NAMESIZE * 2) + 8)
42+
#define EXENAME_SIZE ((PATH_MAX * 2) + 1)
4243

4344
// NOTE: The kernel fills in pid, uid, and loginuid of sender. Therefore,
4445
// these routines do not need to send them.
@@ -179,8 +180,15 @@ static char *_get_exename(char *exename, int size)
179180
audit_msg(LOG_ERR, "get_exename: cannot determine executable");
180181
} else {
181182
tmp[res] = '\0';
182-
if (audit_value_needs_encoding(tmp, res))
183+
if (audit_value_needs_encoding(tmp, res)) {
184+
if (((res * 2) + 1) > size) {
185+
strcpy(exename, "\"?\"");
186+
audit_msg(LOG_ERR,
187+
"get_exename: encoded executable name too long");
188+
return exename;
189+
}
183190
return audit_encode_value(exename, tmp, res);
191+
}
184192
snprintf(exename, size, "\"%s\"", tmp);
185193
}
186194
return exename;
@@ -294,7 +302,7 @@ int audit_log_user_message(int audit_fd, int type, const char *message,
294302
{
295303
char buf[MAX_AUDIT_MESSAGE_LENGTH];
296304
char addrbuf[INET6_ADDRSTRLEN];
297-
static char exename[PATH_MAX*2]="";
305+
static char exename[EXENAME_SIZE]="";
298306
char ttyname[TTY_PATH];
299307
const char *success;
300308
int ret;
@@ -371,7 +379,7 @@ int audit_log_user_comm_message(int audit_fd, int type, const char *message,
371379
{
372380
char buf[MAX_AUDIT_MESSAGE_LENGTH];
373381
char addrbuf[INET6_ADDRSTRLEN];
374-
static char exename[PATH_MAX*2]="";
382+
static char exename[EXENAME_SIZE]="";
375383
char commname[PATH_MAX*2];
376384
char ttyname[TTY_PATH];
377385
const char *success;
@@ -456,7 +464,7 @@ int audit_log_acct_message(int audit_fd, int type, const char *pgname,
456464
const char *success;
457465
char buf[MAX_AUDIT_MESSAGE_LENGTH];
458466
char addrbuf[INET6_ADDRSTRLEN];
459-
static char exename[PATH_MAX*2] = "";
467+
static char exename[EXENAME_SIZE] = "";
460468
char ttyname[TTY_PATH];
461469
int ret;
462470

@@ -558,7 +566,7 @@ int audit_log_user_avc_message(int audit_fd, int type, const char *message,
558566
{
559567
char buf[MAX_AUDIT_MESSAGE_LENGTH];
560568
char addrbuf[INET6_ADDRSTRLEN];
561-
static char exename[PATH_MAX*2] = "";
569+
static char exename[EXENAME_SIZE] = "";
562570
char ttyname[TTY_PATH];
563571
int retval;
564572

@@ -639,7 +647,7 @@ int audit_log_semanage_message(int audit_fd, int type, const char *pgname,
639647
const char *success;
640648
char buf[MAX_AUDIT_MESSAGE_LENGTH];
641649
char addrbuf[INET6_ADDRSTRLEN];
642-
static char exename[PATH_MAX*2] = "";
650+
static char exename[EXENAME_SIZE] = "";
643651
char ttyname[TTY_PATH];
644652
int ret;
645653

@@ -746,7 +754,7 @@ int audit_log_user_command(int audit_fd, int type, const char *command,
746754
char commname[PATH_MAX*2];
747755
char cwdname[PATH_MAX*2];
748756
char ttyname[TTY_PATH];
749-
static char exename[PATH_MAX*2] = "";
757+
static char exename[EXENAME_SIZE] = "";
750758
char format[64];
751759
const char *success;
752760
char *cmd;
@@ -832,4 +840,3 @@ int audit_log_user_command(int audit_fd, int type, const char *command,
832840
errno = ret;
833841
return ret;
834842
}
835-

0 commit comments

Comments
 (0)