Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
PATCH: Stack-based buffer overflow in exec.c:hashcmd()
- X-seq: zsh-workers 42518
- From: Oliver Kiddle <okiddle@xxxxxxxxxxx>
- To: Zsh workers <zsh-workers@xxxxxxx>
- Subject: PATCH: Stack-based buffer overflow in exec.c:hashcmd()
- Date: Sat, 24 Mar 2018 14:02:37 +0100
- Authentication-results: amavisd4.gkg.net (amavisd-new); dkim=pass (2048-bit key) header.d=yahoo.co.uk
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.co.uk; s=s2048; t=1521899927; bh=ZJoYUBOuPFj98VNVoXk8cgGS+4kwE8CY+Un11SljM5Y=; h=From:To:Subject:Date:From:Subject; b=LkkdcVReSC7UdWhZBH9Ymv2zK3+0K2FqrYZoo5ATN5eYB0W/rZPnbFYPJHMspFyU4gXE8F85awFjfvv4Yu5ZQmT0Ry0+evWod2fpjXAt1VcsArSFHRVhdZj86+qwOb1QE6tMccUrCbece0ayABH+mqlbMxn6mZ63PKKvIpSYfi7FID7cGq6GzdrESHW/p18P44u8/zYHGJw8aGH3t92ZpKSqQ54PVGjP6Aplyw5Rp3sbU0qS2yZ5Yzd5njvAX8MSD+3mFnxidNq6/c9U430nB0cHV7jdQ+dzsGV8hSKzdDvKV1BbxXJx6MG1z8MZll+/IbddhevkEsggfH7jk7SPag==
- List-help: <mailto:zsh-workers-help@zsh.org>
- List-id: Zsh Workers List <zsh-workers.zsh.org>
- List-post: <mailto:zsh-workers@zsh.org>
- List-unsubscribe: <mailto:zsh-workers-unsubscribe@zsh.org>
- Mailing-list: contact zsh-workers-help@xxxxxxx; run by ezmlm
The code in exec.c:hashcmd() alocates a PATH_MAX+1 sized buffer but
doesn't check for overflows when copying the path strings which come
from $PATH. This bug corresponds to CVE-2018-1071 and was reported
off-list.
The copy is done using strucpy() which is like strcpy(3) except that
we increment the pointer for reasons of efficency. We also have a
struncpy() function but it is unconditionally copying n bytes before
adding a null. This isn't especially helpful - you could just add n
to the initial pointer to gain the efficiency and writing n+1 bytes
is asking for trouble. So this changes struncpy() to be closer to the
strncpy(3) analogue except I don't see the point in padding the whole
buffer with nulls. There was only one existing use of struncpy() in
exec.c:search_defpath().
diff --git a/Src/exec.c b/Src/exec.c
index 35b0bb191..e154d1249 100644
--- a/Src/exec.c
+++ b/Src/exec.c
@@ -934,7 +934,7 @@ hashcmd(char *arg0, char **pp)
for (; *pp; pp++)
if (**pp == '/') {
s = buf;
- strucpy(&s, *pp);
+ struncpy(&s, *pp, PATH_MAX);
*s++ = '/';
if ((s - buf) + strlen(arg0) >= PATH_MAX)
continue;
diff --git a/Src/utils.c b/Src/utils.c
index 3b589aa35..998b16220 100644
--- a/Src/utils.c
+++ b/Src/utils.c
@@ -2283,10 +2283,10 @@ struncpy(char **s, char *t, int n)
{
char *u = *s;
- while (n--)
- *u++ = *t++;
+ while (n-- && (*u++ = *t++));
*s = u;
- *u = '\0';
+ if (n > 0) /* just one null-byte will do, unlike strncpy(3) */
+ *u = '\0';
}
/* Return the number of elements in an array of pointers. *
Messages sorted by:
Reverse Date,
Date,
Thread,
Author