bug-coreutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

patch to who and uptime to fix some non-null-terminated string code


From: Paul Eggert
Subject: patch to who and uptime to fix some non-null-terminated string code
Date: Sat, 24 Jul 2004 01:13:32 -0700
User-agent: Gnus/5.1006 (Gnus v5.10.6) Emacs/21.3 (gnu/linux)

I installed this patch to fix some bugs in 'who' and 'uptime'
involving strings that aren't null-terminated.  The 'uptime' fix
seems pretty theoretical, but the 'who' bug could actually bite.

I introduced the 'who' bug in the 2004-06-19 patch that removed the
truncation of user names at 8 bytes.  I didn't realize that the old
code assumed that sizeof UT_USER was 8, and that the proper fix was to
adjust the format precision to be sizeof UT_USER rather than to remove
the precision entirely.  Sorry about that.

2004-07-24  Paul Eggert  <address@hidden>

        * src/uptime.c (print_uptime) [defined BOOT_MSG]:
        Don't assume ut_line is null-terminated.
        * src/who.c (print_line): New arguments USERLEN and LINELEN,
        since USER and LINE might not be null terminated.  All callers
        changed.
 
Index: src/uptime.c
===================================================================
RCS file: /home/eggert/coreutils/cu/src/uptime.c,v
retrieving revision 1.42
diff -p -u -r1.42 uptime.c
--- src/uptime.c        12 Jul 2004 17:55:06 -0000      1.42
+++ src/uptime.c        24 Jul 2004 06:49:00 -0000
@@ -115,7 +115,7 @@ print_uptime (int n, const STRUCT_UTMP *
 # if HAVE_PROC_UPTIME
       if (uptime == 0)
 # endif /* HAVE_PROC_UPTIME */
-       if (STREQ (this->ut_line, BOOT_MSG))
+       if (strncmp (this->ut_line, BOOT_MSG, sizeof this->ut_line) == 0)
          boot_time = UT_TIME_MEMBER (this);
 #endif /* BOOT_MSG */
       ++this;
Index: src/who.c
===================================================================
RCS file: /home/eggert/coreutils/cu/src/who.c,v
retrieving revision 1.99
diff -p -u -r1.99 who.c
--- src/who.c   23 Jun 2004 14:48:16 -0000      1.99
+++ src/who.c   24 Jul 2004 07:35:25 -0000
@@ -259,7 +259,8 @@ time_string (const STRUCT_UTMP *utmp_ent
    will need tweaking if any of the localization stuff is done, or for 64 bit
    pids, etc. */
 static void
-print_line (const char *user, const char state, const char *line,
+print_line (int userlen, const char *user, const char state,
+           int linelen, const char *line,
            const char *time_str, const char *idle, const char *pid,
            const char *comment, const char *exitstr)
 {
@@ -289,18 +290,18 @@ print_line (const char *user, const char
     *x_exitstr = '\0';
 
   err = asprintf (&buf,
-                 "%-8s"
+                 "%-8.*s"
                  "%s"
-                 " %-12s"
+                 " %-12.*s"
                  " %-*s"
                  "%s"
                  "%s"
                  " %-8s"
                  "%s"
                  ,
-                 user ? user : "   .",
+                 userlen, user ? user : "   .",
                  include_mesg ? mesg : "",
-                 line,
+                 linelen, line,
                  time_format_width,
                  time_str,
                  x_idle,
@@ -432,7 +433,8 @@ print_user (const STRUCT_UTMP *utmp_ent,
     }
 #endif
 
-  print_line (UT_USER (utmp_ent), mesg, utmp_ent->ut_line,
+  print_line (sizeof UT_USER (utmp_ent), UT_USER (utmp_ent), mesg,
+             sizeof utmp_ent->ut_line, utmp_ent->ut_line,
              time_string (utmp_ent), idlestr, pidstr,
              hoststr ? hoststr : "", "");
 }
@@ -440,7 +442,8 @@ print_user (const STRUCT_UTMP *utmp_ent,
 static void
 print_boottime (const STRUCT_UTMP *utmp_ent)
 {
-  print_line ("", ' ', "system boot", time_string (utmp_ent), "", "", "", "");
+  print_line (-1, "", ' ', -1, "system boot",
+             time_string (utmp_ent), "", "", "", "");
 }
 
 static char *
@@ -471,7 +474,7 @@ print_deadprocs (const STRUCT_UTMP *utmp
 
   /* FIXME: add idle time? */
 
-  print_line ("", ' ', utmp_ent->ut_line,
+  print_line (-1, "", ' ', sizeof utmp_ent->ut_line, utmp_ent->ut_line,
              time_string (utmp_ent), "", pidstr, comment, exitstr);
   free (comment);
 }
@@ -484,7 +487,7 @@ print_login (const STRUCT_UTMP *utmp_ent
 
   /* FIXME: add idle time? */
 
-  print_line ("LOGIN", ' ', utmp_ent->ut_line,
+  print_line (-1, "LOGIN", ' ', sizeof utmp_ent->ut_line, utmp_ent->ut_line,
              time_string (utmp_ent), "", pidstr, comment, "");
   free (comment);
 }
@@ -495,7 +498,7 @@ print_initspawn (const STRUCT_UTMP *utmp
   char *comment = make_id_equals_comment (utmp_ent);
   PIDSTR_DECL_AND_INIT (pidstr, utmp_ent);
 
-  print_line ("", ' ', utmp_ent->ut_line,
+  print_line (-1, "", ' ', sizeof utmp_ent->ut_line, utmp_ent->ut_line,
              time_string (utmp_ent), "", pidstr, comment, "");
   free (comment);
 }
@@ -504,7 +507,7 @@ static void
 print_clockchange (const STRUCT_UTMP *utmp_ent)
 {
   /* FIXME: handle NEW_TIME & OLD_TIME both */
-  print_line ("", ' ', _("clock change"),
+  print_line (-1, "", ' ', -1, _("clock change"),
              time_string (utmp_ent), "", "", "", "");
 }
 
@@ -523,7 +526,7 @@ print_runlevel (const STRUCT_UTMP *utmp_
     comment = xmalloc (strlen (_("last=")) + 2);
   sprintf (comment, "%s%c", _("last="), (last == 'N') ? 'S' : last);
 
-  print_line ("", ' ', runlevline, time_string (utmp_ent),
+  print_line (-1, "", ' ', -1, runlevline, time_string (utmp_ent),
              "", "", comment, "");
 
   return;
@@ -558,8 +561,8 @@ list_entries_who (int n, const STRUCT_UT
 static void
 print_heading (void)
 {
-  print_line (_("NAME"), ' ', _("LINE"), _("TIME"), _("IDLE"), _("PID"),
-             _("COMMENT"), _("EXIT"));
+  print_line (-1, _("NAME"), ' ', -1, _("LINE"), _("TIME"), _("IDLE"),
+             _("PID"), _("COMMENT"), _("EXIT"));
 }
 
 /* Display UTMP_BUF, which should have N entries. */




reply via email to

[Prev in Thread] Current Thread [Next in Thread]