Source-Changes-HG archive

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

[src/trunk]: src check for snprintf() truncation and fail sanely if so, rathe...



details:   https://anonhg.NetBSD.org/src/rev/dbafa43941b3
branches:  trunk
changeset: 448504:dbafa43941b3
user:      mrg <mrg%NetBSD.org@localhost>
date:      Mon Feb 04 08:21:11 2019 +0000

description:
check for snprintf() truncation and fail sanely if so, rather than
attempting to use a file that won't exist or isn't secure.

diffstat:

 lib/libintl/gettext.c        |  10 ++++++----
 sbin/iscsid/iscsid_main.c    |   9 +++++++--
 usr.sbin/npf/npfd/npfd_log.c |   8 +++++---
 3 files changed, 18 insertions(+), 9 deletions(-)

diffs (99 lines):

diff -r f22ed15bea8b -r dbafa43941b3 lib/libintl/gettext.c
--- a/lib/libintl/gettext.c     Mon Feb 04 08:18:07 2019 +0000
+++ b/lib/libintl/gettext.c     Mon Feb 04 08:21:11 2019 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: gettext.c,v 1.29 2015/05/29 12:26:28 christos Exp $    */
+/*     $NetBSD: gettext.c,v 1.30 2019/02/04 08:21:11 mrg Exp $ */
 
 /*-
  * Copyright (c) 2000, 2001 Citrus Project,
@@ -29,7 +29,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: gettext.c,v 1.29 2015/05/29 12:26:28 christos Exp $");
+__RCSID("$NetBSD: gettext.c,v 1.30 2019/02/04 08:21:11 mrg Exp $");
 
 #include <sys/param.h>
 #include <sys/stat.h>
@@ -329,8 +329,10 @@
                        continue;
 #endif
 
-               snprintf(buf, len, "%s/%s/%s/%s.mo", dir, p,
+               int rv = snprintf(buf, len, "%s/%s/%s/%s.mo", dir, p,
                    category, domainname);
+               if (rv > (int)len)
+                       return NULL;
                if (stat(buf, &st) < 0)
                        continue;
                if ((st.st_mode & S_IFMT) != S_IFREG)
@@ -942,7 +944,7 @@
           unsigned long int n, int category)
 {
        const char *msgid;
-       char path[PATH_MAX];
+       char path[PATH_MAX+1];
        const char *lpath;
        static char olpath[PATH_MAX];
        const char *cname = NULL;
diff -r f22ed15bea8b -r dbafa43941b3 sbin/iscsid/iscsid_main.c
--- a/sbin/iscsid/iscsid_main.c Mon Feb 04 08:18:07 2019 +0000
+++ b/sbin/iscsid/iscsid_main.c Mon Feb 04 08:21:11 2019 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: iscsid_main.c,v 1.11 2016/05/30 21:58:32 mlelstv Exp $ */
+/*     $NetBSD: iscsid_main.c,v 1.12 2019/02/04 08:21:12 mrg Exp $     */
 
 /*-
  * Copyright (c) 2005,2006,2011 The NetBSD Foundation, Inc.
@@ -90,6 +90,7 @@
        uint32_t hid = 0;
        size_t siz;
        int mib[2];
+       int total;
        unsigned char *s;
 
        (void) memset(&snp, 0x0, sizeof(snp));
@@ -109,8 +110,12 @@
        for (s = snp.InitiatorAlias; *s; s++)
                if (!isalnum((unsigned char) *s) && *s != '-' && *s != '.' && *s != ':')
                        *s = '-';
-       snprintf((char *)snp.InitiatorName, sizeof(snp.InitiatorName),
+       total = snprintf((char *)snp.InitiatorName, sizeof(snp.InitiatorName),
                "iqn.1994-04.org.netbsd:iscsi.%s:%u", snp.InitiatorAlias, hid);
+       if ((size_t)total > sizeof(snp.InitiatorName)) {
+               printf("Warning: iSCSI Node InitiatorName too long to set InitiatorAlias!\n");
+               return ISCSID_STATUS_NO_INITIATOR_NAME;
+       }
 
        ioctl(driver, ISCSI_SET_NODE_NAME, &snp);
        return snp.status;
diff -r f22ed15bea8b -r dbafa43941b3 usr.sbin/npf/npfd/npfd_log.c
--- a/usr.sbin/npf/npfd/npfd_log.c      Mon Feb 04 08:18:07 2019 +0000
+++ b/usr.sbin/npf/npfd/npfd_log.c      Mon Feb 04 08:21:11 2019 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: npfd_log.c,v 1.12 2017/10/16 11:17:45 christos Exp $   */
+/*     $NetBSD: npfd_log.c,v 1.13 2019/02/04 08:21:12 mrg Exp $        */
 
 /*-
  * Copyright (c) 2015 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: npfd_log.c,v 1.12 2017/10/16 11:17:45 christos Exp $");
+__RCSID("$NetBSD: npfd_log.c,v 1.13 2019/02/04 08:21:12 mrg Exp $");
 
 #include <sys/types.h>
 #include <sys/param.h>
@@ -190,7 +190,9 @@
 rename:
        fclose(fp);
        char tmp[MAXPATHLEN];
-       snprintf(tmp, sizeof(tmp), "%s.XXXXXX", ctx->path);
+       if (snprintf(tmp, sizeof(tmp), "%s.XXXXXX", ctx->path) > MAXPATHLEN)
+               syslog(LOG_ERR, "Temp file truncated: `%s' does not fit",
+                      ctx->path);
        int fd;
        if ((fd = mkstemp(tmp)) == -1) {
                syslog(LOG_ERR, "Can't make temp file `%s': %m", tmp);



Home | Main Index | Thread Index | Old Index