Author: markj
Date: Thu Aug 27 23:46:45 2015
New Revision: 287228
URL: https://svnweb.freebsd.org/changeset/base/287228

Log:
  MFC r286169:
  Perform bounds checking when constructing a format string.
  
  PR:   201657

Modified:
  stable/9/cddl/contrib/opensolaris/lib/libdtrace/common/dt_printf.c
Directory Properties:
  stable/9/cddl/contrib/opensolaris/   (props changed)
  stable/9/cddl/contrib/opensolaris/lib/libdtrace/common/   (props changed)

Modified: stable/9/cddl/contrib/opensolaris/lib/libdtrace/common/dt_printf.c
==============================================================================
--- stable/9/cddl/contrib/opensolaris/lib/libdtrace/common/dt_printf.c  Thu Aug 
27 23:46:42 2015        (r287227)
+++ stable/9/cddl/contrib/opensolaris/lib/libdtrace/common/dt_printf.c  Thu Aug 
27 23:46:45 2015        (r287228)
@@ -1348,6 +1348,7 @@ dt_printf_format(dtrace_hdl_t *dtp, FILE
        dtrace_aggdesc_t *agg;
        caddr_t lim = (caddr_t)buf + len, limit;
        char format[64] = "%";
+       size_t ret;
        int i, aggrec, curagg = -1;
        uint64_t normal;
 
@@ -1379,7 +1380,9 @@ dt_printf_format(dtrace_hdl_t *dtp, FILE
                int prec = pfd->pfd_prec;
                int rval;
 
+               const char *start;
                char *f = format + 1; /* skip initial '%' */
+               size_t fmtsz = sizeof(format) - 1;
                const dtrace_recdesc_t *rec;
                dt_pfprint_f *func;
                caddr_t addr;
@@ -1536,6 +1539,7 @@ dt_printf_format(dtrace_hdl_t *dtp, FILE
                        break;
                }
 
+               start = f;
                if (pfd->pfd_flags & DT_PFCONV_ALT)
                        *f++ = '#';
                if (pfd->pfd_flags & DT_PFCONV_ZPAD)
@@ -1548,6 +1552,7 @@ dt_printf_format(dtrace_hdl_t *dtp, FILE
                        *f++ = '\'';
                if (pfd->pfd_flags & DT_PFCONV_SPACE)
                        *f++ = ' ';
+               fmtsz -= f - start;
 
                /*
                 * If we're printing a stack and DT_PFCONV_LEFT is set, we
@@ -1558,13 +1563,20 @@ dt_printf_format(dtrace_hdl_t *dtp, FILE
                if (func == pfprint_stack && (pfd->pfd_flags & DT_PFCONV_LEFT))
                        width = 0;
 
-               if (width != 0)
-                       f += snprintf(f, sizeof (format), "%d", ABS(width));
+               if (width != 0) {
+                       ret = snprintf(f, fmtsz, "%d", ABS(width));
+                       f += ret;
+                       fmtsz = MAX(0, fmtsz - ret);
+               }
 
-               if (prec > 0)
-                       f += snprintf(f, sizeof (format), ".%d", prec);
+               if (prec > 0) {
+                       ret = snprintf(f, fmtsz, ".%d", prec);
+                       f += ret;
+                       fmtsz = MAX(0, fmtsz - ret);
+               }
 
-               (void) strcpy(f, pfd->pfd_fmt);
+               if (strlcpy(f, pfd->pfd_fmt, fmtsz) >= fmtsz)
+                       return (dt_set_errno(dtp, EDT_COMPILER));
                pfd->pfd_rec = rec;
 
                if (func(dtp, fp, format, pfd, addr, size, normal) < 0)
_______________________________________________
[email protected] mailing list
https://lists.freebsd.org/mailman/listinfo/svn-src-stable-9
To unsubscribe, send any mail to "[email protected]"

Reply via email to