Skip to contentSkip to conversation

mingw: do not crash on open(NULL, ...)

PATCH Applied Erik Faye-Lund 5 participants 12 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mingw: do not crash on open(NULL, ...)

Since open() already sets errno correctly for the NULL-case, let's just avoid the problematic strcmp.

---
 compat/mingw.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
compat/mingw.c +1 -1
index b0ad919..b408c3c 100644
@@ -297,7 +297,7 @@ int mingw_open (const char *filename, int oflags, ...)
 	mode = va_arg(args, int);
 	va_end(args);
 
-	if (!strcmp(filename, "/dev/null"))
+	if (filename && !strcmp(filename, "/dev/null"))
 		filename = "nul";
 
 	fd = open(filename, oflags, mode);
Signature · 1 line
1.7.2.3.msysgit.0.207.gda0b3
Acked-by on v1 1/1
#1 lore raw

compat/mingw.c int mingw_open (const char *filename, int oflags, ...) · line 303

Quoted 9 lines · Erik Faye-Lund
compat/mingw.c +1 -1
@@ -297,7 +297,7 @@ int mingw_open (const char *filename, int oflags, ...)
 	mode = va_arg(args, int);
 	va_end(args);
 
-	if (!strcmp(filename, "/dev/null"))
+	if (filename && !strcmp(filename, "/dev/null"))
 		filename = "nul";
 
 	fd = open(filename, oflags, mode);

Good catch, thank you!

-- Hannes

Applied
#2 lore raw
Quoted 9 lines · Johannes Sixt
Quoted 8 lines · Erik Faye-Lund
compat/mingw.c +1 -1
@@ -297,7 +297,7 @@ int mingw_open (const char *filename, int oflags, ...)
       mode = va_arg(args, int);
       va_end(args);
 
-     if (!strcmp(filename, "/dev/null"))
+     if (filename && !strcmp(filename, "/dev/null"))
               filename = "nul";
 
Quoted 7 lines

compat/mingw.c int mingw_open (const char *filename, int oflags, ...) · line 303

Quoted 1 line
compat/mingw.c +0 -0
       fd = open(filename, oflags, mode);

Good catch, thank you!

-- Hannes

Applied to devel and pushed.

#3 lore raw
In reply to Erik Faye-Lund
Quoted 4 lines · Erik Faye-Lund

Since open() already sets errno correctly for the NULL-case, let's just avoid the problematic strcmp.

I guess I should add a comment as to why this patch is needed:

This seems to be the culprit for issue 523 in the msysGit issue
tracker: http://code.google.com/p/msysgit/issues/detail?id=523

fetch_and_setup_pack_index() apparently pass a NULL-pointer to parse_pack_index(), which in turn pass it to check_packed_git_idx(), which again pass it to open(). This all looks intentional to my (http.c-untrained) eye.

The code in mingw_open was introduced in commit 3e4a1ba (by Johannes Sixt), and the lack of a NULL-check looks like a simple oversight.

#4 lore raw
Quoted 8 lines · Erik Faye-Lund
Quoted 4 lines · Erik Faye-Lund

Since open() already sets errno correctly for the NULL-case, let's just avoid the problematic strcmp.

I guess I should add a comment as to why this patch is needed:

Quoted 7 lines

This seems to be the culprit for issue 523 in the msysGit issue
tracker: http://code.google.com/p/msysgit/issues/detail?id=523

fetch_and_setup_pack_index() apparently pass a NULL-pointer to parse_pack_index(), which in turn pass it to check_packed_git_idx(), which again pass it to open(). This all looks intentional to my (http.c-untrained) eye.

Surely, open(NULL) should be rejected by a sane system, and your patch looks sane to me.

But depending on and exploiting the fact sounds like a horrible hack in the caller of parse_pack_index(..., NULL) to me.

Shawn may have intentionally done that in 750ef42 (http-fetch: Use temporary files for pack-*.idx until verified, 2010-04-19), but at least 7b64469 (Allow parse_pack_index on temporary files, 2010-04-19) should have documented that idx_path is allowed to be NULL under what circumstance (and for what purpose it is useful to do so) when it introduced the second parameter to the API.

What were we smoking?

#5 lore raw
Quoted 13 lines · Junio C Hamano
Quoted 11 lines · Erik Faye-Lund
Quoted 4 lines · Erik Faye-Lund

Since open() already sets errno correctly for the NULL-case, let's just avoid the problematic strcmp.

I guess I should add a comment as to why this patch is needed:

This seems to be the culprit for issue 523 in the msysGit issue
tracker: http://code.google.com/p/msysgit/issues/detail?id=523

Quoted 8 lines
Quoted 4 lines

fetch_and_setup_pack_index() apparently pass a NULL-pointer to parse_pack_index(), which in turn pass it to check_packed_git_idx(), which again pass it to open(). This all looks intentional to my (http.c-untrained) eye.

Surely, open(NULL) should be rejected by a sane system, and your patch looks sane to me.

Since this doesn't seem to be in git.git yet, perhaps you could squash this on top? I didn't notice it in time, but fopen lacked the same check (freopen already had the check). It's not as important, because it doesn't seem like we have any code reaching this path so far, but it would IMO be better to fix this now rather than having to chase down the issue again later...

compat/mingw.c +1 -1
index 4595aaa..f069fea 100644
@@ -160,7 +160,7 @@ ssize_t mingw_write(int fd, const void *buf, size_t count)
 #undef fopen
 FILE *mingw_fopen (const char *filename, const char *otype)
 {
-	if (!strcmp(filename, "/dev/null"))
+	if (filename && !strcmp(filename, "/dev/null"))
 		filename = "nul";
 	return fopen(filename, otype);
 }
#6 lore raw
Quoted 36 lines · Erik Faye-Lund
Quoted 13 lines · Junio C Hamano
Quoted 11 lines · Erik Faye-Lund
Quoted 4 lines · Erik Faye-Lund

Since open() already sets errno correctly for the NULL-case, let's just avoid the problematic strcmp.

I guess I should add a comment as to why this patch is needed:

This seems to be the culprit for issue 523 in the msysGit issue
tracker: http://code.google.com/p/msysgit/issues/detail?id=523

Quoted 8 lines
Quoted 4 lines

fetch_and_setup_pack_index() apparently pass a NULL-pointer to parse_pack_index(), which in turn pass it to check_packed_git_idx(), which again pass it to open(). This all looks intentional to my (http.c-untrained) eye.

Surely, open(NULL) should be rejected by a sane system, and your patch looks sane to me.

Since this doesn't seem to be in git.git yet, perhaps you could squash this on top? I didn't notice it in time, but fopen lacked the same check (freopen already had the check). It's not as important, because it doesn't seem like we have any code reaching this path so far, but it would IMO be better to fix this now rather than having to chase down the issue again later...

compat/mingw.c +0 -0
index 4595aaa..f069fea 100644

compat/mingw.c ssize_t mingw_write(int fd, const void *buf, size_t count) · line 166

Quoted 8 lines
compat/mingw.c +1 -1
@@ -160,7 +160,7 @@ ssize_t mingw_write(int fd, const void *buf, size_t count)
  #undef fopen
  FILE *mingw_fopen (const char *filename, const char *otype)
  {
-       if (!strcmp(filename, "/dev/null"))
+       if (filename && !strcmp(filename, "/dev/null"))
                filename = "nul";
        return fopen(filename, otype);
  }

I'll apply this to the devel branch and try to remember to squash it on the next rebase-merge.
Cheers,
Pat

#7 lore raw
Quoted 42 lines · Pat Thoyts
Quoted 41 lines · Erik Faye-Lund
Quoted 13 lines · Junio C Hamano
Quoted 11 lines · Erik Faye-Lund
Quoted 4 lines · Erik Faye-Lund

Since open() already sets errno correctly for the NULL-case, let's just avoid the problematic strcmp.

I guess I should add a comment as to why this patch is needed:

This seems to be the culprit for issue 523 in the msysGit issue
tracker: http://code.google.com/p/msysgit/issues/detail?id=523

Quoted 8 lines
Quoted 4 lines

fetch_and_setup_pack_index() apparently pass a NULL-pointer to parse_pack_index(), which in turn pass it to check_packed_git_idx(), which again pass it to open(). This all looks intentional to my (http.c-untrained) eye.

Surely, open(NULL) should be rejected by a sane system, and your patch looks sane to me.

Since this doesn't seem to be in git.git yet, perhaps you could squash this on top? I didn't notice it in time, but fopen lacked the same check (freopen already had the check). It's not as important, because it doesn't seem like we have any code reaching this path so far, but it would IMO be better to fix this now rather than having to chase down the issue again later...

compat/mingw.c +1 -1
index 4595aaa..f069fea 100644
@@ -160,7 +160,7 @@ ssize_t mingw_write(int fd, const void *buf, size_t count)
  #undef fopen
  FILE *mingw_fopen (const char *filename, const char *otype)
  {
-       if (!strcmp(filename, "/dev/null"))
+       if (filename && !strcmp(filename, "/dev/null"))
                filename = "nul";
Quoted 9 lines

compat/mingw.c ssize_t mingw_write(int fd, const void *buf, size_t count) · line 166

Quoted 3 lines
compat/mingw.c +0 -0
        return fopen(filename, otype);
  }

I'll apply this to the devel branch and try to remember to squash it on the next rebase-merge.
Cheers,
Pat

Wouldn't it be better to just get this squashed in git.git, and drop the patch in the next rebase-merge? Since this bug is in git.git as well, it makes sense to get the patch merged there, no?

#8 lore raw
In reply to Pat Thoyts

Hi,

Quoted 40 lines · Pat Thoyts
Quoted 39 lines · Erik Faye-Lund
Quoted 13 lines · Junio C Hamano
Quoted 11 lines · Erik Faye-Lund
Quoted 4 lines · Erik Faye-Lund

Since open() already sets errno correctly for the NULL-case, let's just avoid the problematic strcmp.

I guess I should add a comment as to why this patch is needed:

This seems to be the culprit for issue 523 in the msysGit issue
tracker: http://code.google.com/p/msysgit/issues/detail?id=523

Quoted 8 lines
Quoted 4 lines

fetch_and_setup_pack_index() apparently pass a NULL-pointer to parse_pack_index(), which in turn pass it to check_packed_git_idx(), which again pass it to open(). This all looks intentional to my (http.c-untrained) eye.

Surely, open(NULL) should be rejected by a sane system, and your patch looks sane to me.

Since this doesn't seem to be in git.git yet, perhaps you could squash this on top? I didn't notice it in time, but fopen lacked the same check (freopen already had the check). It's not as important, because it doesn't seem like we have any code reaching this path so far, but it would IMO be better to fix this now rather than having to chase down the issue again later...

compat/mingw.c +0 -1
index 4595aaa..f069fea 100644
@@ -160,7 +160,7 @@ ssize_t mingw_write(int fd, const void *buf, size_t count)
  #undef fopen
  FILE *mingw_fopen (const char *filename, const char *otype)
  {
-       if (!strcmp(filename, "/dev/null"))
Quoted 8 lines

compat/mingw.c ssize_t mingw_write(int fd, const void *buf, size_t count) · line 166

Quoted 5 lines
compat/mingw.c +1 -0
+       if (filename && !strcmp(filename, "/dev/null"))
                filename = "nul";
        return fopen(filename, otype);
  }

I'll apply this to the devel branch and try to remember to squash it on the next rebase-merge.

Usually I mark commits like this with ("amend deadcafe") in the commit subject so I do not forget... ;-)

Ciao,
Dscho

#9 lore raw
In reply to Erik Faye-Lund

(Apparently gmail on a phone insists on top posting)
It looks like this problem was missed by the test suite. Any chance of a test as well? Got to catch those regressions.

Quoted 1 line · Erik Faye-Lund

On Thu, Sep 23, 2010 at 10:35 AM, Erik Faye-Lund <[email protected]>

Quoted 17 lines · wrote:
Quoted 4 lines

Since open() already sets errno correctly for the NULL-case, let's just avoid the problematic strcmp.

I guess I should add a comment as to why this patch is needed:

This seems to be the culprit for issue 523 in the msysGit issue
tracker: http://code.google.com/p/msysgit/issues/detail?id=523

fetch_and_setup_pack_index() apparently pass a NULL-pointer to parse_pack_index(), which in turn pass it to check_packed_git_idx(), which again pass it to open(). This all looks intentional to my (http.c-untrained) eye.

The code in mingw_open was introduced in commit 3e4a1ba (by Johannes Sixt), and the lack of a NULL-check looks like a simple oversight.

#10 lore raw
Quoted 1 line · Pat Thoyts

(Apparently gmail on a phone insists on top posting)

Fixed ;)

Quoted 18 lines · Erik Faye-Lund
Quoted 17 lines
Quoted 4 lines · Erik Faye-Lund

Since open() already sets errno correctly for the NULL-case, let's just avoid the problematic strcmp.

I guess I should add a comment as to why this patch is needed:

This seems to be the culprit for issue 523 in the msysGit issue
tracker: http://code.google.com/p/msysgit/issues/detail?id=523

fetch_and_setup_pack_index() apparently pass a NULL-pointer to parse_pack_index(), which in turn pass it to check_packed_git_idx(), which again pass it to open(). This all looks intentional to my (http.c-untrained) eye.

Quoted 6 lines
Quoted 2 lines

The code in mingw_open was introduced in commit 3e4a1ba (by Johannes Sixt), and the lack of a NULL-check looks like a simple oversight.

It looks like this problem was missed by the test suite. Any chance of a test as well? Got to catch those regressions.

I don't think it's practical to test CRT functions directly, but perhaps a test for parse_pack_index() or some level above that might make sense. I tried looking at the this has been done for sha1_file previously, but it seems there's not really any tests at this level. And all tests for http.c seems to depend on Apache being installed (something we do not have in msysGit), so adding a test at this level wouldn't have helped us any...

In other words, I don't think there is any natural point to add a test for this. Feel free to make suggestions, though.

#11 lore raw
In reply to Erik Faye-Lund

Hi,

Quoted 2 lines · Erik Faye-Lund

The code in mingw_open was introduced in commit 3e4a1ba (by Johannes Sixt), and the lack of a NULL-check looks like a simple oversight.

Erik, you have all the information at your finger tips. So why not Cc:
Johannes Sixt, who you blame so openly, so that he has a chance to react (and less of a chance to miss your criticism)?

Ciao,
Dscho