]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
login: fix segmentation fault in log_utmp
authorKarel Zak <kzak@redhat.com>
Mon, 21 May 2012 07:58:42 +0000 (09:58 +0200)
committerKarel Zak <kzak@redhat.com>
Thu, 24 May 2012 10:34:33 +0000 (12:34 +0200)
 ctx->tty_number is optional...

Addresses: https://bugzilla.redhat.com/show_bug.cgi?id=822705
Signed-off-by: Karel Zak <kzak@redhat.com>
login-utils/login.c

index a984af62e4c9ef246c7e41bd6a420ae1d92107dc..826c2eb8f0ea4eb6faa82dbc4b670512af1cfc37 100644 (file)
@@ -388,9 +388,11 @@ static void init_tty(struct login_context *cxt)
        }
 
 #ifdef LOGIN_CHOWN_VCS
-       /* find names of Virtual Console devices, for later mode change */
-       snprintf(cxt->vcsn, sizeof(cxt->vcsn), "/dev/vcs%s", cxt->tty_number);
-       snprintf(cxt->vcsan, sizeof(cxt->vcsan), "/dev/vcsa%s", cxt->tty_number);
+       if (cxt->tty_number) {
+               /* find names of Virtual Console devices, for later mode change */
+               snprintf(cxt->vcsn, sizeof(cxt->vcsn), "/dev/vcs%s", cxt->tty_number);
+               snprintf(cxt->vcsan, sizeof(cxt->vcsan), "/dev/vcsa%s", cxt->tty_number);
+       }
 #endif
 
        tcgetattr(0, &tt);
@@ -577,7 +579,8 @@ static void log_utmp(struct login_context *cxt)
        if (utp == NULL) {
                setutent();
                ut.ut_type = LOGIN_PROCESS;
-               strncpy(ut.ut_line, cxt->tty_name, sizeof(ut.ut_line));
+               if (cxt->tty_name)
+                       strncpy(ut.ut_line, cxt->tty_name, sizeof(ut.ut_line));
                utp = getutline(&ut);
        }
 
@@ -587,11 +590,12 @@ static void log_utmp(struct login_context *cxt)
                /* some gettys/telnetds don't initialize utmp... */
                memset(&ut, 0, sizeof(ut));
 
-       if (ut.ut_id[0] == 0)
+       if (cxt->tty_number && ut.ut_id[0] == 0)
                strncpy(ut.ut_id, cxt->tty_number, sizeof(ut.ut_id));
-
-       strncpy(ut.ut_user, cxt->username, sizeof(ut.ut_user));
-       xstrncpy(ut.ut_line, cxt->tty_name, sizeof(ut.ut_line));
+       if (cxt->username)
+               strncpy(ut.ut_user, cxt->username, sizeof(ut.ut_user));
+       if (cxt->tty_name)
+               xstrncpy(ut.ut_line, cxt->tty_name, sizeof(ut.ut_line));
 
 #ifdef _HAVE_UT_TV             /* in <utmpbits.h> included by <utmp.h> */
        gettimeofday(&tv, NULL);
@@ -624,6 +628,9 @@ static void log_syslog(struct login_context *cxt)
 {
        struct passwd *pwd = cxt->pwd;
 
+       if (!cxt->tty_name)
+               return;
+
        if (!strncmp(cxt->tty_name, "ttyS", 4))
                syslog(LOG_INFO, _("DIALUP AT %s BY %s"),
                       cxt->tty_name, pwd->pw_name);