git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH] color: protect against out-of-bounds array access/assignment

From
Jonathan Nieder <jrnieder@gmail.com>
Date
Aug 3, 2018, 06:07 UTC
Message-ID
<20180803060749.GA237521@aiede.svl.corp.google.com>
In-Reply-To
<20180802093208.27420-1-sunshine@sunshineco.com>
Hi,
Eric Sunshine wrote:
Show 21 quoted lines
> want_color_fd() is designed to work only with standard input, output,
> and error file descriptors, and stores information about each descriptor
> in an array. However, it doesn't verify that the passed-in descriptor
> lives within that set, which, with a buggy caller, could lead to
> access/assignment outside the array bounds.
>
> Signed-off-by: Eric Sunshine <sunshine@sunshineco.com>
> ---
>  color.c | 3 +++
>  1 file changed, 3 insertions(+)
>
> diff --git a/color.c b/color.c
> index b1c24c69de..b0be9ce505 100644
> --- a/color.c
> +++ b/color.c
> @@ -343,6 +343,9 @@ int want_color_fd(int fd, int var)
>  
>  	static int want_auto[3] = { -1, -1, -1 };
>  
> +	if (fd < 0 || fd >= ARRAY_SIZE(want_auto))
> +	    BUG("file descriptor out of range: %d", fd);
The indentation looks wrong here.

Combining that with the other suggestions from this thread, I end up with this v2:

-- >8 --
From: Eric Sunshine <sunshine@sunshineco.com>
Subject: color: protect against out-of-bounds reads and writes

want_color_fd() is designed to work only with standard output and error file descriptors and stores information about each descriptor in an array. However, it doesn't verify that the passed-in descriptor lives within that set, which, with a buggy caller, could lead to access or assignment outside the array bounds.

Signed-off-by: Eric Sunshine <sunshine@sunshineco.com>
Acked-by: Johannes Schindelin <Johannes.Schindelin@gmx.de>
Signed-off-by: Jonathan Nieder <jrnieder@gmail.com>
---
 color.c | 3 +++
 1 file changed, 3 insertions(+)
diff --git a/color.c b/color.c
index b1c24c69de..ebb222ec33 100644
--- a/color.c
+++ b/color.c
@@ -343,6 +343,9 @@ int want_color_fd(int fd, int var)
 
 	static int want_auto[3] = { -1, -1, -1 };
 
+	if (fd < 1 || fd >= ARRAY_SIZE(want_auto))
+		BUG("file descriptor out of range: %d", fd);
+
 	if (var < 0)
 		var = git_use_color_default;
 
-- 
2.18.0.597.ga71716f1ad
Previous: Jeff KingNext: Eric Sunshine
Message 7 of 8 in “color: protect against out-of-bounds array access/assignment”
  1. color: protect against out-of-bounds array access/assignmentEric Sunshine, Aug 2, 2018
  2. Johannes SchindelinAug 2, 2018
  3. Junio C HamanoAug 2, 2018
  4. Eric SunshineAug 2, 2018
  5. Junio C HamanoAug 2, 2018
  6. Jeff KingAug 2, 2018
  7. Jonathan NiederAug 3, 2018
  8. Eric SunshineAug 3, 2018

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.