NetBSD Problem Report #21741

Received: (qmail 27253 invoked by uid 605); 31 May 2003 20:51:48 -0000
Message-Id: <20030531225141.79a57ec8.christianbiere@gmx.de>
Date: Sat, 31 May 2003 22:51:41 +0200
From: Christian Biere <christianbiere@gmx.de>
Sender: gnats-bugs-owner@netbsd.org
To: gnats-bugs@gnats.netbsd.org
Cc: markn@greenwoodsoftware.com
Subject: Buffer overrun and wrong usage of  islower() in less 381

>Number:         21741
>Category:       bin
>Synopsis:       Buffer overrun and wrong usage of  islower() in less 381
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    bin-bug-people
>State:          closed
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Sat May 31 20:52:00 +0000 2003
>Closed-Date:    Sat Sep 23 12:51:40 +0000 2006
>Last-Modified:  Wed Sep 27 11:30:04 +0000 2006
>Originator:     Christian Biere
>Release:        NetBSD 1.6R
>Organization:
>Environment:

>Description:
There's a possible buffer overrun in tags.c in findgtag(). If
the environment variable LESSGLOBALTAGS is large enough, sprintf() will
cause a buffer overrun of the local variable `command' which can hold
512 chars.
The functions islower() and other ctype functions (resp. macros) are not
used correctly, thus causing undefined behaviour on machines with char
being signed char.
The bugs above can be fixed by the attached patch.


However, there's a further problem:
If defined the environment variable LESSBINFMT is used as second
parameter (the format string) for sprintf(). The used buffer can hold
only 8 chars. This limit is not mentioned anywhere in the man page
and LESSBINFMT can cause all kinds of havoc sprintf() can cause if used
"incorrectly".

>How-To-Repeat:


>Fix:


--Multipart_Sat__31_May_2003_22:51:41_+0200_081bd000
Content-Type: text/plain;
 name="less.udif"
Content-Disposition: attachment;
 filename="less.udif"
Content-Transfer-Encoding: 7bit

--- command.c	2003/05/31 17:02:06	1.1
+++ command.c	2003/05/31 19:09:21
@@ -203,7 +203,7 @@
 			every_first_cmd = save(cbuf);
 		break;
 	case A_OPT_TOGGLE:
-		toggle_option(optchar, cbuf, optflag);
+		toggle_option((unsigned char) optchar, cbuf, optflag);
 		optchar = '\0';
 		break;
 	case A_F_BRACKET:
@@ -403,7 +403,7 @@
 				if (cmd_char(c) == CC_QUIT)
 					return (MCA_DONE);
 				p = get_cmdbuf();
-				lc = islower(p[0]);
+				lc = islower((unsigned char) p[0]);
 				o = findopt_name(&p, &oname, NULL);
 				if (o != NULL)
 				{
@@ -413,15 +413,15 @@
 					 * display the full option name.
 					 */
 					optchar = o->oletter;
-					if (!lc && islower(optchar))
-						optchar = toupper(optchar);
+					if (!lc && islower((unsigned char) optchar))
+						optchar = toupper((unsigned char) optchar);
 					cmd_reset();
 					mca_opt_toggle();
 					for (p = oname;  *p != '\0';  p++)
 					{
 						c = *p;
-						if (!lc && islower(c))
-							c = toupper(c);
+						if (!lc && islower((unsigned char) c))
+							c = toupper((unsigned char) c);
 						if (cmd_char(c) != CC_OK)
 							return (MCA_DONE);
 					}
--- opttbl.c	2003/05/31 19:12:34	1.1
+++ opttbl.c	2003/05/31 19:12:52
@@ -454,7 +454,7 @@
 	{
 		if (o->oletter == c)
 			return (o);
-		if ((o->otype & TRIPLE) && toupper(o->oletter) == c)
+		if ((o->otype & TRIPLE) && toupper((unsigned char) o->oletter) == c)
 			return (o);
 	}
 	return (NULL);
--- tags.c	2003/05/31 17:06:02	1.1
+++ tags.c	2003/05/31 20:12:38
@@ -498,7 +498,7 @@
 #if !HAVE_POPEN
 		return TAG_NOFILE;
 #else
-		char command[512];
+		char *command;
 		char *flag;
 		char *qtag;
 		char *cmd = lgetenv("LESSGLOBALTAGS");
@@ -528,10 +528,12 @@
 		qtag = shell_quote(tag);
 		if (qtag == NULL)
 			qtag = tag;
+		command = (char *) ecalloc(strlen(cmd)+strlen(flag)+strlen(qtag)+5, sizeof(char));
 		sprintf(command, "%s -x%s %s", cmd, flag, qtag);
 		if (qtag != tag)
 			free(qtag);
 		fp = popen(command, "r");
+		free(command);
 #endif
 	}
 	if (fp != NULL)
@@ -539,6 +541,7 @@
 		while (fgets(buf, sizeof(buf), fp))
 		{
 			char *name, *file, *line;
+			int len;

 			if (sigs)
 			{
@@ -548,8 +551,8 @@
 #endif
 				return TAG_INTR;
 			}
-			if (buf[strlen(buf) - 1] == '\n')
-				buf[strlen(buf) - 1] = 0;
+			if ((len = strlen(buf)) && buf[len - 1] == '\n')
+				buf[len - 1] = 0;
 			else
 			{
 				int c;
@@ -712,12 +715,12 @@
 {
 	char *p = buf;

-	for (*tag = p;  *p && !isspace(*p);  p++)	/* tag name */
+	for (*tag = p;  *p && !isspace((unsigned char) *p);  p++)	/* tag name */
 		;
 	if (*p == 0)
 		return (-1);
 	*p++ = 0;
-	for ( ;  *p && isspace(*p);  p++)		/* (skip blanks) */
+	for ( ;  *p && isspace((unsigned char) *p);  p++)		/* (skip blanks) */
 		;
 	if (*p == 0)
 		return (-1);
@@ -725,27 +728,27 @@
 	 * If the second part begin with other than digit,
 	 * it is assumed tag type. Skip it.
 	 */
-	if (!isdigit(*p))
+	if (!isdigit((unsigned char) *p))
 	{
-		for ( ;  *p && !isspace(*p);  p++)	/* (skip tag type) */
+		for ( ;  *p && !isspace((unsigned char) *p);  p++)	/* (skip tag type) */
 			;
-		for (;  *p && isspace(*p);  p++)	/* (skip blanks) */
+		for (;  *p && isspace((unsigned char) *p);  p++)	/* (skip blanks) */
 			;
 	}
-	if (!isdigit(*p))
+	if (!isdigit((unsigned char) *p))
 		return (-1);
 	*line = p;					/* line number */
-	for (*line = p;  *p && !isspace(*p);  p++)
+	for (*line = p;  *p && !isspace((unsigned char) *p);  p++)
 		;
 	if (*p == 0)
 		return (-1);
 	*p++ = 0;
-	for ( ; *p && isspace(*p);  p++)		/* (skip blanks) */
+	for ( ; *p && isspace((unsigned char) *p);  p++)		/* (skip blanks) */
 		;
 	if (*p == 0)
 		return (-1);
 	*file = p;					/* file name */
-	for (*file = p;  *p && !isspace(*p);  p++)
+	for (*file = p;  *p && !isspace((unsigned char) *p);  p++)
 		;
 	if (*p == 0)
 		return (-1);

--Multipart_Sat__31_May_2003_22:51:41_+0200_081bd000--
>Release-Note:
>Audit-Trail:

Date: Thu, 12 Jun 2003 10:46:55 -0700
From: "Mark Nudelman" <markn@greenwoodsoftware.com>
To: "Christian Biere" <christianbiere@gmx.de>, <gnats-bugs@gnats.netbsd.org>
Subject: Re: Buffer overrun and wrong usage of  islower() in less 381
References: <20030531225141.79a57ec8.christianbiere@gmx.de>

 Christian,
 Thank you for sending this bug report and patch.  I will include these fixes
 in the next release of less.
 --Mark

State-Changed-From-To: open->closed
State-Changed-By: elad@netbsd.org
State-Changed-When: Sat, 23 Sep 2006 12:51:40 +0000
State-Changed-Why:
i applied the patch, thanks!


From: Elad Efrat <elad@netbsd.org>
To: gnats-bugs@NetBSD.org
Cc: 
Subject: PR/21741 CVS commit: src/usr.bin/less/less
Date: Sat, 23 Sep 2006 12:51:18 +0000 (UTC)

 Module Name:	src
 Committed By:	elad
 Date:		Sat Sep 23 12:51:18 UTC 2006

 Modified Files:
 	src/usr.bin/less/less: command.c tags.c

 Log Message:
 PR/21741: Christian Biere: Buffer overrun and wrong usage of  islower() in
 less 381
 Over three years ago the less maintainer said he'll add these fixes.
 Nothing happened, so we do it ourselves.
 Thanks for the report and the patch!


 To generate a diff of this commit:
 cvs rdiff -r1.11 -r1.12 src/usr.bin/less/less/command.c
 cvs rdiff -r1.6 -r1.7 src/usr.bin/less/less/tags.c

 Please note that diffs are not public domain; they are subject to the
 copyright notices on the relevant files.

From: Takahiro Kambe <taca@netbsd.org>
To: gnats-bugs@NetBSD.org
Cc: 
Subject: PR/21741 CVS commit: pkgsrc/misc/ja-less
Date: Wed, 27 Sep 2006 11:27:31 +0000 (UTC)

 Module Name:	pkgsrc
 Committed By:	taca
 Date:		Wed Sep 27 11:27:31 UTC 2006

 Modified Files:
 	pkgsrc/misc/ja-less: Makefile distinfo
 Added Files:
 	pkgsrc/misc/ja-less/patches: patch-ad patch-ae

 Log Message:
 Apply changes from NetBSD current approved by wiz@:

 ----------------------------------------------------------------------

 Module Name:	src
 Committed By:	elad
 Date:		Sat Sep 23 12:51:18 UTC 2006

 Modified Files:
 	src/usr.bin/less/less: command.c tags.c

 Log Message:
 PR 21741: Christian Biere: Buffer overrun and wrong usage of  islower() in
 less 381
 Over three years ago the less maintainer said he'll add these fixes.
 Nothing happened, so we do it ourselves.
 Thanks for the report and the patch!

 ----------------------------------------------------------------------

 Bump PKGREVISION.


 To generate a diff of this commit:
 cvs rdiff -r1.6 -r1.7 pkgsrc/misc/ja-less/Makefile
 cvs rdiff -r1.2 -r1.3 pkgsrc/misc/ja-less/distinfo
 cvs rdiff -r0 -r1.1 pkgsrc/misc/ja-less/patches/patch-ad \
     pkgsrc/misc/ja-less/patches/patch-ae

 Please note that diffs are not public domain; they are subject to the
 copyright notices on the relevant files.

From: Takahiro Kambe <taca@netbsd.org>
To: gnats-bugs@NetBSD.org
Cc: 
Subject: PR/21741 CVS commit: pkgsrc/misc/less
Date: Wed, 27 Sep 2006 11:28:32 +0000 (UTC)

 Module Name:	pkgsrc
 Committed By:	taca
 Date:		Wed Sep 27 11:28:32 UTC 2006

 Modified Files:
 	pkgsrc/misc/less: Makefile distinfo
 Added Files:
 	pkgsrc/misc/less/patches: patch-aa patch-ab

 Log Message:
 Apply changes from NetBSD current approved by wiz@:

 ----------------------------------------------------------------------

 Module Name:	src
 Committed By:	elad
 Date:		Sat Sep 23 12:51:18 UTC 2006

 Modified Files:
 	src/usr.bin/less/less: command.c tags.c

 Log Message:
 PR 21741: Christian Biere: Buffer overrun and wrong usage of  islower() in
 less 381
 Over three years ago the less maintainer said he'll add these fixes.
 Nothing happened, so we do it ourselves.
 Thanks for the report and the patch!

 ----------------------------------------------------------------------

 Bump PKGREVISION.


 To generate a diff of this commit:
 cvs rdiff -r1.14 -r1.15 pkgsrc/misc/less/Makefile
 cvs rdiff -r1.8 -r1.9 pkgsrc/misc/less/distinfo
 cvs rdiff -r0 -r1.1 pkgsrc/misc/less/patches/patch-aa \
     pkgsrc/misc/less/patches/patch-ab

 Please note that diffs are not public domain; they are subject to the
 copyright notices on the relevant files.

>Unformatted:

NetBSD Home
NetBSD PR Database Search

(Contact us) $NetBSD: query-full-pr,v 1.51 2026/08/10 02:28:17 riastradh Exp $
$NetBSD: gnats_config.sh,v 1.10 2026/05/13 22:00:09 riastradh Exp $
Copyright © 1994-2026 The NetBSD Foundation, Inc. ALL RIGHTS RESERVED.