@@ -69,6 +69,28 @@ struct eventlog_args {
6969 void * json_info ;
7070};
7171
72+ /*
73+ * Escape control characters in str the same way new_logline() does.
74+ * Returns an allocated string that the caller must free, else NULL.
75+ */
76+ static char *
77+ escape_cntrl (const char * str )
78+ {
79+ struct sudo_lbuf lbuf ;
80+ debug_decl (escape_cntrl , SUDO_DEBUG_UTIL );
81+
82+ sudo_lbuf_init (& lbuf , NULL , 0 , NULL , 0 );
83+ if (!sudo_lbuf_append_esc (& lbuf , LBUF_ESC_CNTRL , "%s" , str )) {
84+ sudo_lbuf_destroy (& lbuf );
85+ debug_return_str (NULL );
86+ }
87+ if (lbuf .buf == NULL ) {
88+ /* Nothing was allocated for the empty string. */
89+ debug_return_str (strdup ("" ));
90+ }
91+ debug_return_str (lbuf .buf );
92+ }
93+
7294/*
7395 * Allocate and fill in a new logline.
7496 */
@@ -1029,9 +1051,10 @@ static bool
10291051do_syslog_sudo (int pri , char * logline , const struct eventlog * evlog )
10301052{
10311053 const struct eventlog_config * evl_conf = eventlog_getconf ();
1054+ char * p , * submituser = NULL , * tmp , save ;
10321055 size_t len , maxlen ;
1033- char * p , * tmp , save ;
10341056 const char * fmt ;
1057+ bool ret = true;
10351058 debug_decl (do_syslog_sudo , SUDO_DEBUG_UTIL );
10361059
10371060 evl_conf -> open_log (EVLOG_SYSLOG , NULL );
@@ -1042,12 +1065,19 @@ do_syslog_sudo(int pri, char *logline, const struct eventlog *evlog)
10421065 goto done ;
10431066 }
10441067
1068+ /* Escape control characters, the submit user may be untrusted. */
1069+ if ((submituser = escape_cntrl (evlog -> submituser )) == NULL ) {
1070+ sudo_warnx (U_ ("%s: %s" ), __func__ , U_ ("unable to allocate memory" ));
1071+ ret = false;
1072+ goto done ;
1073+ }
1074+
10451075 /*
10461076 * Log the full line, breaking into multiple syslog(3) calls if necessary
10471077 */
10481078 fmt = _ ("%8s : %s" );
10491079 maxlen = evl_conf -> syslog_maxlen -
1050- (strlen (fmt ) - 5 + strlen (evlog -> submituser ));
1080+ (strlen (fmt ) - 5 + strlen (submituser ));
10511081 for (p = logline ; * p != '\0' ; ) {
10521082 len = strlen (p );
10531083 if (len > maxlen ) {
@@ -1063,25 +1093,26 @@ do_syslog_sudo(int pri, char *logline, const struct eventlog *evlog)
10631093 save = * tmp ;
10641094 * tmp = '\0' ;
10651095
1066- syslog (pri , fmt , evlog -> submituser , p );
1096+ syslog (pri , fmt , submituser , p );
10671097
10681098 * tmp = save ; /* restore saved character */
10691099
10701100 /* Advance p and eliminate leading whitespace */
10711101 for (p = tmp ; * p == ' ' ; p ++ )
10721102 continue ;
10731103 } else {
1074- syslog (pri , fmt , evlog -> submituser , p );
1104+ syslog (pri , fmt , submituser , p );
10751105 p += len ;
10761106 }
10771107 fmt = _ ("%8s : (command continued) %s" );
10781108 maxlen = evl_conf -> syslog_maxlen -
1079- (strlen (fmt ) - 5 + strlen (evlog -> submituser ));
1109+ (strlen (fmt ) - 5 + strlen (submituser ));
10801110 }
10811111done :
1112+ free (submituser );
10821113 evl_conf -> close_log (EVLOG_SYSLOG , NULL );
10831114
1084- debug_return_bool (true );
1115+ debug_return_bool (ret );
10851116}
10861117
10871118static bool
@@ -1187,6 +1218,7 @@ do_logfile_sudo(const char *logline, const struct eventlog *evlog,
11871218 char * full_line , timebuf [8192 ], * timestr = NULL ;
11881219 const char * timefmt = evl_conf -> time_fmt ;
11891220 const char * logfile = evl_conf -> logpath ;
1221+ char * submituser = NULL ;
11901222 struct tm tm ;
11911223 bool ret = false;
11921224 FILE * fp ;
@@ -1214,8 +1246,13 @@ do_logfile_sudo(const char *logline, const struct eventlog *evlog,
12141246 }
12151247 }
12161248 if (evlog != NULL ) {
1249+ /* Escape control characters, the submit user may be untrusted. */
1250+ if ((submituser = escape_cntrl (evlog -> submituser )) == NULL ) {
1251+ sudo_warnx (U_ ("%s: %s" ), __func__ , U_ ("unable to allocate memory" ));
1252+ goto done ;
1253+ }
12171254 len = asprintf (& full_line , "%s : %s : %s" ,
1218- timestr ? timestr : "invalid date" , evlog -> submituser , logline );
1255+ timestr ? timestr : "invalid date" , submituser , logline );
12191256 } else {
12201257 len = asprintf (& full_line , "%s : %s" ,
12211258 timestr ? timestr : "invalid date" , logline );
@@ -1235,6 +1272,7 @@ do_logfile_sudo(const char *logline, const struct eventlog *evlog,
12351272 ret = true;
12361273
12371274done :
1275+ free (submituser );
12381276 (void )sudo_lock_file (fileno (fp ), SUDO_UNLOCK );
12391277 evl_conf -> close_log (EVLOG_FILE , fp );
12401278 debug_return_bool (ret );
0 commit comments