Your message dated Sun, 10 Jul 2016 14:00:13 +0000
with message-id <[email protected]>
and subject line Bug#830313: fixed in procps 2:3.3.12-1
has caused the Debian Bug report #830313,
regarding watch: segfaults with --color
to be marked as done.

This means that you claim that the problem has been dealt with.
If this is not the case it is now your responsibility to reopen the
Bug report if necessary, and/or fix the problem forthwith.

(NB: If you are a system administrator and have no idea what this
message is talking about, this may indicate a serious mail system
misconfiguration somewhere. Please contact [email protected]
immediately.)


-- 
830313: http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=830313
Debian Bug Tracking System
Contact [email protected] with problems
--- Begin Message ---
Package: procps
Version: 2:3.3.11-3
Severity: normal
File: /usr/bin/watch
Tags: upstream patch

A minimal test case that reproduces this:
watch -c -t '/usr/bin/printf "\x1b[m"'

The underlying bug involves a read of an uninitialized pointer in the
implementation of --color (so the method of reproducing the segfault may
vary).

The implementation of --color calls the following function whenever it
encounters an escape character ('\033'):

static void process_ansi(FILE * fp)
{
        int i, c;
        char buf[MAX_ANSIBUF];
        char *numstart, *endptr;

        c = getc(fp);
        if (c != '[') {
                ungetc(c, fp);
                return;
        }
        for (i = 0; i < MAX_ANSIBUF; i++) {
                c = getc(fp);
                /* COLOUR SEQUENCE ENDS in 'm' */
                if (c == 'm') {
                        buf[i] = '\0';
                        break;
                }
                if (c < '0' && c > '9' && c != ';') {
                        while (--i >= 0)
                                ungetc(buf[i], fp);
                        return;
                }
                buf[i] = (char)c;
        }
        /*
         * buf now contains a semicolon-separated list of decimal integers,
         * each indicating an attribute to apply.
         * For example, buf might contain "0;1;31", derived from the color
         * escape sequence "<ESC>[0;1;31m". There can be 1 or more
         * attributes to apply, but typically there are between 1 and 3.
         */

        if (*endptr == '\0') set_ansi_attribute(0); /* [m treated as [0m */

        for (endptr = numstart = buf; *endptr != '\0'; numstart = endptr + 1)
                set_ansi_attribute(strtol(numstart, &endptr, 10));
}

This function has several problems:

- The if near the end dereferences *endptr without initializing it.
  This causes the segfault.

- The condition (c < '0' && c > '9' && c != ';') will never match, so
  this will happily read MAX_ANSIBUF characters unless it encounters a
  'm'.  (This will also produce interesting results when c == EOF.)
  This should use ((c < '0' || c > '9') && c != ';').

- Even with that condition fixed, this does not ungetc the character it
  just read, nor does it ungetc the '[' character or the '\x1b' escape
  character.

- This assumes, unportably, that it can ungetc more than one character;
  "man ungetc" specifically says "only one pushback is guaranteed", and
  in fact glibc only supports one character.  (In any case, the rest of
  the escape sequence should ideally be skipped, not printed; that will
  also fix the previous issue of not ungetting some of the characters.)

- This assumes all numbers in a color control sequence correspond to
  colors or attributes, which will breaks badly if it encounters a
  ISO-8613-3 escape sequence (such as for truecolor RGB).  For instance,
  the sequence "\x1b[38;2;10;20;30m" sets the foreground color to
  rgb(10,20,30), but watch will interpret all five numbers in the
  sequence as colors or attributes themselves.  watch should stop
  processing the entire escape sequence if it encounters something it
  doesn't understand.  (I'd suggest doing this by having
  set_ansi_attribute return a bool, with false meaning "skip the rest".)

If not for ncurses, I would suggest using the behavior of "less -R":
matching ANSI escape sequences, discarding non-color sequences, and
passing through color sequences assuming they don't move the cursor.
However, that won't work with ncurses.

I've attached a series of git patches that fix the problems noted above.

- Josh Triplett
>From c41c19d92da12b32a73222c7a6b286570e3eecb7 Mon Sep 17 00:00:00 2001
From: Josh Triplett <[email protected]>
Date: Fri, 8 Jul 2016 00:19:37 -0700
Subject: [PATCH 1/4] watch: Fix segfault in --color handling

When process_ansi processed a color sequence, it would always read from
an uninitialized pointer, which would sometimes cause a segfault.  Fix
to read from the correct pointer.

Signed-off-by: Josh Triplett <[email protected]>
---
 watch.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/watch.c b/watch.c
index de46730..a75f3c0 100644
--- a/watch.c
+++ b/watch.c
@@ -228,7 +228,7 @@ static void process_ansi(FILE * fp)
 	 * attributes to apply, but typically there are between 1 and 3.
 	 */
 
-	if (*endptr == '\0') set_ansi_attribute(0); /* [m treated as [0m */
+	if (*buf == '\0') set_ansi_attribute(0); /* [m treated as [0m */
 
 	for (endptr = numstart = buf; *endptr != '\0'; numstart = endptr + 1)
 		set_ansi_attribute(strtol(numstart, &endptr, 10));
-- 
2.8.1

>From a4013bf67e2f01a59d777619445b0507a9611219 Mon Sep 17 00:00:00 2001
From: Josh Triplett <[email protected]>
Date: Fri, 8 Jul 2016 00:21:55 -0700
Subject: [PATCH 2/4] watch: Don't process additional numbers in unknown ANSI
 color escapes

process_ansi assumed all numbers in a color control sequence correspond
to colors or attributes, which breaks badly if it encounters a
ISO-8613-3 escape sequence (such as for truecolor RGB).  For instance,
the sequence "\x1b[38;2;10;20;30m" sets the foreground color to
rgb(10,20,30), but watch will interpret all five numbers in the sequence
as colors or attributes themselves.

Stop processing the entire escape sequence if watch encounters any
number it doesn't understand, as that number may change the meaning of
the rest of the sequence.

Signed-off-by: Josh Triplett <[email protected]>
---
 watch.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/watch.c b/watch.c
index a75f3c0..118cb8b 100644
--- a/watch.c
+++ b/watch.c
@@ -136,7 +136,7 @@ static void init_ansi_colors(void)
 }
 
 
-static void set_ansi_attribute(const int attrib)
+static int set_ansi_attribute(const int attrib)
 {
 	switch (attrib) {
 	case -1:	/* restore last settings */
@@ -189,10 +189,13 @@ static void set_ansi_attribute(const int attrib)
 			fg_col = attrib - 30 + 1;
 		} else if (attrib >= 40 && attrib <= 47) { /* set background color */
 			bg_col = attrib - 40 + 1;
+		} else {
+			return 0; /* Not understood */
 		}
 	}
 
 	attrset(attributes | COLOR_PAIR(bg_col * nr_of_colors + fg_col + 1));
+	return 1;
 }
 
 static void process_ansi(FILE * fp)
@@ -231,7 +234,8 @@ static void process_ansi(FILE * fp)
 	if (*buf == '\0') set_ansi_attribute(0); /* [m treated as [0m */
 
 	for (endptr = numstart = buf; *endptr != '\0'; numstart = endptr + 1)
-		set_ansi_attribute(strtol(numstart, &endptr, 10));
+		if (!set_ansi_attribute(strtol(numstart, &endptr, 10)))
+			break;
 }
 
 static void __attribute__ ((__noreturn__)) do_exit(int status)
-- 
2.8.1

>From 4590116cca8c0200680f4a7cdefb9443c3c28eb6 Mon Sep 17 00:00:00 2001
From: Josh Triplett <[email protected]>
Date: Fri, 8 Jul 2016 00:29:59 -0700
Subject: [PATCH 3/4] watch: Fix ANSI escape sequence termination

process_ansi stopped processing an ANSI escape sequence if
(c < '0' && c > '9' && c != ';'), which will never happen.  Fix the
range check to use || instead.

Signed-off-by: Josh Triplett <[email protected]>
---
 watch.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/watch.c b/watch.c
index 118cb8b..56b5b29 100644
--- a/watch.c
+++ b/watch.c
@@ -216,7 +216,7 @@ static void process_ansi(FILE * fp)
 			buf[i] = '\0';
 			break;
 		}
-		if (c < '0' && c > '9' && c != ';') {
+		if ((c < '0' || c > '9') && c != ';') {
 			while (--i >= 0)
 				ungetc(buf[i], fp);
 			return;
-- 
2.8.1

>From 597aff834ef7ff59a5b760b6461002f910798cf3 Mon Sep 17 00:00:00 2001
From: Josh Triplett <[email protected]>
Date: Fri, 8 Jul 2016 00:32:59 -0700
Subject: [PATCH 4/4] watch: Don't attempt to ungetc parts of unknown ANSI
 escape sequences

If process_ansi encountered an unknown character when processing an ANSI
escape sequence, it would ungetc all the characters read so far, except
for the character just read, and the opening '\033['.  ungetting the
middle of the escape sequence does not produce useful results, and also
relies on the unportable assumption that ungetc works on multiple
characters (which glibc does not support).  Discard the characters
instead.

Signed-off-by: Josh Triplett <[email protected]>
---
 watch.c | 2 --
 1 file changed, 2 deletions(-)

diff --git a/watch.c b/watch.c
index 56b5b29..8978dd7 100644
--- a/watch.c
+++ b/watch.c
@@ -217,8 +217,6 @@ static void process_ansi(FILE * fp)
 			break;
 		}
 		if ((c < '0' || c > '9') && c != ';') {
-			while (--i >= 0)
-				ungetc(buf[i], fp);
 			return;
 		}
 		buf[i] = (char)c;
-- 
2.8.1


--- End Message ---
--- Begin Message ---
Source: procps
Source-Version: 2:3.3.12-1

We believe that the bug you reported is fixed in the latest version of
procps, which is due to be installed in the Debian FTP archive.

A summary of the changes between this version and the previous one is
attached.

Thank you for reporting the bug, which will now be closed.  If you
have further comments please address them to [email protected],
and the maintainer will reopen the bug report if appropriate.

Debian distribution maintenance software
pp.
Craig Small <[email protected]> (supplier of updated procps package)

(This message was generated automatically at their request; if you
believe that there is a problem with it please contact the archive
administrators by mailing [email protected])


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA512

Format: 1.8
Date: Sun, 10 Jul 2016 17:39:28 +1000
Source: procps
Binary: procps libprocps6 libprocps-dev
Architecture: source amd64
Version: 2:3.3.12-1
Distribution: unstable
Urgency: medium
Maintainer: Craig Small <[email protected]>
Changed-By: Craig Small <[email protected]>
Description:
 libprocps-dev - library for accessing process information from /proc
 libprocps6 - library for accessing process information from /proc
 procps     - /proc file system utilities
Closes: 692113 692279 733172 755233 770215 786956 804966 812394 816237 823080 
827423 830313
Changes:
 procps (2:3.3.12-1) unstable; urgency=medium
 .
   [ Helmut Grohne <[email protected]> ]
   * Fix FTBFS pass --host to configure Closes: #812394
 .
   [ Craig Small ]
   * New upstream version
   - free: man document rewritten for shared Closes: #755233
   - free: interpret intervals in non-locale way Closes: #692113
   - library: find tty quicker Closes: #770215
   - kill: report error if cannot kill process Closes: #733172
   - ps: sort by cgroup Closes: #692279
   - ps: fallback to attr/current for context Closes: #786956
   - tests: conditionally add prctl Closes: #816237
   - watch: better handling ANSI including esc[m Closes: #830313
   - watch: use locale-independent float Closes: #692113
   * Fix FTBFS for hurd Closes: #816237
   * Dropped initscript dependency Closes: #804966
   * Don't start procps on install Closes: #827423
   * Fix LSB init script patch from Guillem Closes: #823080
   * Update to standards 3.9.8
Checksums-Sha1:
 17a8b0b8bb013eed8e60f7b5adc345d7665f31cb 2136 procps_3.3.12-1.dsc
 ff473ea8b3bc995b5caa4e68819e8a35cc565a16 840540 procps_3.3.12.orig.tar.xz
 fb18cb62b1f6c75a3e2e7bb6d8886ab400635fa4 26444 procps_3.3.12-1.debian.tar.xz
 3a38d87972479d715842c84f49ba957da7bc1719 69782 libprocps-dev_3.3.12-1_amd64.deb
 19ab820ea757ce8101ce681922ba774f60bbe638 71266 
libprocps6-dbgsym_3.3.12-1_amd64.deb
 a59140b45a3a800ddd54ccacf5699cd39bab47e4 57746 libprocps6_3.3.12-1_amd64.deb
 e83767bb20761167cc4da3906d0481323811b628 335884 
procps-dbgsym_3.3.12-1_amd64.deb
 f95d56c898c9cf36bd2677985c912279e6e35ce0 250030 procps_3.3.12-1_amd64.deb
Checksums-Sha256:
 2b7be33cca3a75e229f7cd8a4aeb6115fa62ad48907025f157abc575d9faf5b1 2136 
procps_3.3.12-1.dsc
 042fcc93e1850aee4c193c51c03f369293fb64fe47e37b38852be6693d12a3af 840540 
procps_3.3.12.orig.tar.xz
 42c33b97fb7ffbf5b16b71194c2a14acc44579723bb2b7a5fc7b0441aaeacf48 26444 
procps_3.3.12-1.debian.tar.xz
 2e0a045ac1a630a30576937b2f5c67de067b13345894b59134dcbf62abca04f1 69782 
libprocps-dev_3.3.12-1_amd64.deb
 6907bf51a5a32594cc6de18d74d83ea087af304f74c03d87824b03993dc2cfc6 71266 
libprocps6-dbgsym_3.3.12-1_amd64.deb
 f644ac219ccfbe6f2cf0d5598667d935ea351a4eca9a84fa186588cf9cd753e5 57746 
libprocps6_3.3.12-1_amd64.deb
 4f856f5d158d1fe682f252cf7bf81ca93ed67994e1e88ac9216247665cda72fd 335884 
procps-dbgsym_3.3.12-1_amd64.deb
 b5ddf2e1d1943c69dfd22d3b5289fe1afda193b5d55f3a6310c2da6d497ee9da 250030 
procps_3.3.12-1_amd64.deb
Files:
 3808bffa631d180fc6ba653e0c51eca3 2136 admin important procps_3.3.12-1.dsc
 8816d9cb9b860a2b5256cd6f48618ddb 840540 admin important 
procps_3.3.12.orig.tar.xz
 2d3517410cad0c745568cb03d1b98924 26444 admin important 
procps_3.3.12-1.debian.tar.xz
 2a58ac594c560233cfa8f13b6362dfaa 69782 libdevel optional 
libprocps-dev_3.3.12-1_amd64.deb
 6e7ff0a8882d09e7f8d82b8f22e87503 71266 debug extra 
libprocps6-dbgsym_3.3.12-1_amd64.deb
 0f67249b284fdbdb461971db11176fe7 57746 libs important 
libprocps6_3.3.12-1_amd64.deb
 949940f3b359dd07f20777df9d734d46 335884 debug extra 
procps-dbgsym_3.3.12-1_amd64.deb
 8effc15d357aa348e56bd5a0607e226e 250030 admin important 
procps_3.3.12-1_amd64.deb

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1

iQIcBAEBCgAGBQJXgfyzAAoJEAIhZsD/PITjv04P/A17kF5OmEYo61zaNgHOiFdl
5IsmcRZqaHNdsIr78kvuPpoEf8MFayTqG4luWNAiNeisHq5fVfn7kzzvVZvZ71tr
2TkADtQ3QmMrlYlOVdnNOyhCVW3qiXvs83IzcQRwetMkZtgSw7/5cwxKTH63+tT/
2gcr2/vg8HM7QWKOoGA9CLQA5XQFnOjtG3mIikSQN9yoMCT63ZA5H7xcNsSjEtLn
bBNON6JchojR+9k4Z6NRgTFw3jxBUuRyIQ3EK/zvZWx94lSsQJjX0TtaZlAhdQsg
RTRAQ34g0usJE1r5/b5jy7GO0m/gsA7ACQvxFYvgCuuMkm8D4+4xh+yj9U1AhXRA
IOuMYS3O4V1qqPZZYT0HZ4F96JYcEIGL6gBcbUGOB4hTxHVZ3ubq3gQNkQVYVpOJ
782mpnGwm9slPYJX8wQPR5Jx2IYMJERH2UuyytSIX62Vv8b7MBlphXgaFrUN/Wjm
hUEsDIeXJXwtB5t6+n1iUn2hpH6mGf7lBK4lZQxRBtAwz6G+vTM2Z/2+xhkLcEzC
3GWC1yT0gvK0SLgFRPFMljrsvgf3E2lfKNBXIA54FplmonCH+a/9n5yd9SrZaQ12
7/392t4r7tExzk0hf6VPdSn/P+2JM41xE5uBji+zgB58ypy13AohTxr3C2yJLgAq
8mYxHOdk0HVfW9B8iecz
=f9Wn
-----END PGP SIGNATURE-----

--- End Message ---

Reply via email to