Skip to contentSkip to conversation

value could be NULL in config parser

Janitors New Junio C Hamano 6 participants 15 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

If somebody wants to dip his or her toe in git hacking, and is tempted to send in a "clean up" patch (e.g. whitespace, coding style) that does not really _fix_ anything, please don't.

I have a task of similar complexity (meaning, reasonably easy) that is much more useful and appreciated than clean-up patches for you.

The callback functions that are passed to git_config() need to be audited so that they do not barf when given NULL. Currently, many of them are not safe.

A callback function of git_config() is called when the command reads value from .git/config and friends. The function takes two parameters, var and value. var is never NULL and it is the name of the configuration variable found in the file being read. value could be either string or NULL.

A NULL value is boolean "true". For example, on MS-DOS, you may have something like this:

	[core]

autocrlf

and your callback will be called with var = "core.autocrlf" and value = NULL in such a case.

If you want to fix them (you do not have to do all of them, and if you would like to help, please make one patch per function fixed), the procedure is:

(1) Find calling sites for git_config(). For example, we find

     one in archive-tar.c::write_tar_archive().

        int write_tar_archive(struct archiver_args *args)
        {
                int plen = args->base ? strlen(args->base) : 0;

                git_config(git_tar_config);

                archive_time = args->time;
                verbose = args->verbose;
	...

(2) Look at the function that is passed to git_config().

        static int git_tar_config(const char *var, const char *value)
        {
                if (!strcmp(var, "tar.umask")) {
                        if (!strcmp(value, "user")) {
                                tar_umask = umask(0);
                                umask(tar_umask);
                        } else {
                                tar_umask = git_config_int(var, value);
                        }
                        return 0;
                }
                return git_default_config(var, value);
        }

(3) Let's fix it. If the user's configuration has:

[tar]
umask

it is an illegal configuration, but the code above does not check for NULL, and the second strcmp() would fail. If we guard that strcmp() with a check against NULL, we would be Ok. git_config_int() will correctly barf telling the user that "tar.umask" configuration is wrong.

(4) Then send in a patch. Again, one patch per fixed function, please. The message may look like this:

-- >8 --
[PATCH] archive-tar.c: guard config parser from value=NULL

 archive-tar.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
archive-tar.c +1 -1
index e1bced5..30aa2e2 100644
@@ -222,7 +222,7 @@ static void write_global_extended_header(const unsigned char *sha1)
 static int git_tar_config(const char *var, const char *value)
 {
 	if (!strcmp(var, "tar.umask")) {
-		if (!strcmp(value, "user")) {
+		if (value && !strcmp(value, "user")) {
 			tar_umask = umask(0);
 			umask(tar_umask);
 		} else {
#1 lore raw

PATCH 1/1 archive-tar.c: guard config parser from value=NULL

---
 archive-tar.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
archive-tar.c +1 -1
index e1bced5..30aa2e2 100644
@@ -222,7 +222,7 @@ static void write_global_extended_header(const unsigned char *sha1)
 static int git_tar_config(const char *var, const char *value)
 {
 	if (!strcmp(var, "tar.umask")) {
-		if (!strcmp(value, "user")) {
+		if (value && !strcmp(value, "user")) {
 			tar_umask = umask(0);
 			umask(tar_umask);
 		} else {
Signature · 1 line
1.5.4
#2 lore raw

PATCH 1/1 builtin-gc.c: guard config parser from value=NULL

In reply to Junio C Hamano
---
 builtin-gc.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
builtin-gc.c +1 -1
index ac34788..ad4a75e 100644
@@ -37,7 +37,7 @@ static const char *argv_rerere[] = {"rerere", "gc", NULL};
 static int gc_config(const char *var, const char *value)
 {
 	if (!strcmp(var, "gc.packrefs")) {
-		if (!strcmp(value, "notbare"))
+		if (value && !strcmp(value, "notbare"))
 			pack_refs = -1;
 		else
 			pack_refs = git_config_bool(var, value);
Signature · 1 line
1.5.4
#3 lore raw

PATCH 1/1 remote.c: guard config parser from value=NULL

In reply to Junio C Hamano
---
 remote.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
remote.c +1 -1
index 0e00680..4765815 100644
@@ -276,7 +276,7 @@ static int handle_config(const char *key, const char *value)
 		else
 			error("more than one uploadpack given, using the first");
 	} else if (!strcmp(subkey, ".tagopt")) {
-		if (!strcmp(value, "--no-tags"))
+		if (value && !strcmp(value, "--no-tags"))
 			remote->fetch_tags = -1;
 	} else if (!strcmp(subkey, ".proxy")) {
 		remote->http_proxy = xstrdup(value);
Signature · 1 line
1.5.4
#4 lore raw

Re: [PATCH] remote.c: guard config parser from value=NULL

Quoted 10 lines · Miklos Vajna
---
 remote.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
remote.c +0 -0
index 0e00680..4765815 100644

remote.c static int handle_config(const char *key, const char *value) · line 282

Quoted 8 lines
remote.c +1 -1
@@ -276,7 +276,7 @@ static int handle_config(const char *key, const char *value)
 		else
 			error("more than one uploadpack given, using the first");
 	} else if (!strcmp(subkey, ".tagopt")) {
-		if (!strcmp(value, "--no-tags"))
+		if (value && !strcmp(value, "--no-tags"))
 			remote->fetch_tags = -1;
 	} else if (!strcmp(subkey, ".proxy")) {
 		remote->http_proxy = xstrdup(value);

Function handle_config() has already returned 0 at this point.

#5 lore raw

PATCH 1/1 setup.c: guard config parser from value=NULL

In reply to Junio C Hamano
---
 setup.c |    8 ++++----
 1 files changed, 4 insertions(+), 4 deletions(-)
setup.c +4 -4
index adede16..361825f 100644
@@ -346,13 +346,13 @@ int git_config_perm(const char *var, const char *value)
 {
 	if (value) {
 		int i;
-		if (!strcmp(value, "umask"))
+		if (value && !strcmp(value, "umask"))
 			return PERM_UMASK;
-		if (!strcmp(value, "group"))
+		if (value && !strcmp(value, "group"))
 			return PERM_GROUP;
-		if (!strcmp(value, "all") ||
+		if (value && (!strcmp(value, "all") ||
 		    !strcmp(value, "world") ||
-		    !strcmp(value, "everybody"))
+		    !strcmp(value, "everybody")))
 			return PERM_EVERYBODY;
 		i = atoi(value);
 		if (i > 1)
Signature · 1 line
1.5.4
#6 lore raw

Re: [PATCH] setup.c: guard config parser from value=NULL

setup.c int git_config_perm(const char *var, const char *value) · line 347

Quoted 7 lines · Miklos Vajna
setup.c +0 -0
index adede16..361825f 100644
@@ -346,13 +346,13 @@ int git_config_perm(const char *var, const char *value)
 {
 	if (value) {
            ^^^^^

value is already checked here. No need to check further.

Quoted 15 lines
setup.c +4 -4
 		int i;
-		if (!strcmp(value, "umask"))
+		if (value && !strcmp(value, "umask"))
 			return PERM_UMASK;
-		if (!strcmp(value, "group"))
+		if (value && !strcmp(value, "group"))
 			return PERM_GROUP;
-		if (!strcmp(value, "all") ||
+		if (value && (!strcmp(value, "all") ||
 		    !strcmp(value, "world") ||
-		    !strcmp(value, "everybody"))
+		    !strcmp(value, "everybody")))
 			return PERM_EVERYBODY;
 		i = atoi(value);
 		if (i > 1)
#7 lore raw

Re: [PATCH] setup.c: guard config parser from value=NULL

Quoted 10 lines · Michele Ballabio

setup.c int git_config_perm(const char *var, const char *value) · line 347

Quoted 7 lines · Miklos Vajna
setup.c +0 -0
index adede16..361825f 100644
@@ -346,13 +346,13 @@ int git_config_perm(const char *var, const char *value)
 {
 	if (value) {
            ^^^^^

value is already checked here. No need to check further.

ah, i missed it. should we add some comment about this or just it wasn't obvious to me only? (same true to the other patch where you pointed out the check was not necessary)

thanks,
- VMiklos

#8 lore raw
In reply to Junio C Hamano
Quoted 96 lines · Junio C Hamano

If somebody wants to dip his or her toe in git hacking, and is tempted to send in a "clean up" patch (e.g. whitespace, coding style) that does not really _fix_ anything, please don't.

I have a task of similar complexity (meaning, reasonably easy) that is much more useful and appreciated than clean-up patches for you.

The callback functions that are passed to git_config() need to be audited so that they do not barf when given NULL. Currently, many of them are not safe.

A callback function of git_config() is called when the command reads value from .git/config and friends. The function takes two parameters, var and value. var is never NULL and it is the name of the configuration variable found in the file being read. value could be either string or NULL.

A NULL value is boolean "true". For example, on MS-DOS, you may have something like this:

        [core]
                autocrlf

and your callback will be called with var = "core.autocrlf" and value = NULL in such a case.

If you want to fix them (you do not have to do all of them, and if you would like to help, please make one patch per function fixed), the procedure is:

(1) Find calling sites for git_config(). For example, we find

     one in archive-tar.c::write_tar_archive().

        int write_tar_archive(struct archiver_args *args)
        {
                int plen = args->base ? strlen(args->base) : 0;

                git_config(git_tar_config);

                archive_time = args->time;
                verbose = args->verbose;
        ...

(2) Look at the function that is passed to git_config().

        static int git_tar_config(const char *var, const char *value)
        {
                if (!strcmp(var, "tar.umask")) {
                        if (!strcmp(value, "user")) {
                                tar_umask = umask(0);
                                umask(tar_umask);
                        } else {
                                tar_umask = git_config_int(var, value);
                        }
                        return 0;
                }
                return git_default_config(var, value);
        }

(3) Let's fix it. If the user's configuration has:

[tar]
umask

it is an illegal configuration, but the code above does not check for NULL, and the second strcmp() would fail. If we guard that strcmp() with a check against NULL, we would be Ok. git_config_int() will correctly barf telling the user that "tar.umask" configuration is wrong.

(4) Then send in a patch. Again, one patch per fixed function, please. The message may look like this:

-- >8 --
[PATCH] archive-tar.c: guard config parser from value=NULL

 archive-tar.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
archive-tar.c +1 -2
index e1bced5..30aa2e2 100644
@@ -222,7 +222,7 @@ static void write_global_extended_header(const unsigned char *sha1)
 static int git_tar_config(const char *var, const char *value)
 {
        if (!strcmp(var, "tar.umask")) {
-               if (!strcmp(value, "user")) {
+               if (value && !strcmp(value, "user")) {
                        tar_umask = umask(0);
                        umask(tar_umask);
                } else {
-
Quoted 4 lines

To unsubscribe from this list: send the line "unsubscribe git" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

I can try my hand at that. I will send some patches later today (after work).

-Govind

#9 lore raw
In reply to Junio C Hamano
Quoted 12 lines · Junio C Hamano

A callback function of git_config() is called when the command reads value from .git/config and friends. The function takes two parameters, var and value. var is never NULL and it is the name of the configuration variable found in the file being read. value could be either string or NULL.

A NULL value is boolean "true". For example, on MS-DOS, you may have something like this:

        [core]
                autocrlf
Quoted 7 lines

and your callback will be called with var = "core.autocrlf" and value = NULL in such a case.

If you want to fix them (you do not have to do all of them, and if you would like to help, please make one patch per function fixed), the procedure is:

I think I got all the erroneous ones. I did

find . -name "*.c" | xargs grep git_config\( | awk '{ idx = index($2, ")"); p = substr($2, 12, idx - 12); print p }' | sort | uniq -u

To try and get a list of all the ones that might need updating. I did notice that most functions never check value for null, but they don't directly access them. They pass them off to other methods. As far as I can tell, some of these methods don't validate the NULL. So they will need to be updated.

Question. Wouldn't it reduce the amount of validation we have to do if whoever is calling back checked null and assigned an empty string? If so, we can probably replace all these patches with one patch.

-Govind

#10 lore raw
Quoted 11 lines · Govind Salinas
Quoted 3 lines · Junio C Hamano

If you want to fix them (you do not have to do all of them, and if you would like to help, please make one patch per function fixed), the procedure is:

I think I got all the erroneous ones. I did

find . -name "*.c" | xargs grep git_config\( | awk '{ idx = index($2, ")"); p = substr($2, 12, idx - 12); print p }' | sort | uniq -u

To try and get a list of all the ones that might need updating.

Thanks.

Quoted 9 lines

I did
notice that most functions never check value for null, but they don't directly access them. They pass them off to other methods. As far as I can tell, some of these methods don't validate the NULL. So they will need to be updated.

Question. Wouldn't it reduce the amount of validation we have to do if whoever is calling back checked null and assigned an empty string? If so, we can probably replace all these patches with one patch.

This was discussed in this thread:

http://thread.gmane.org/gmane.comp.version-control.git/72107/

In short we can't do that now, but it may be possible latter, if we first deprecate using an empty string as boolean value "false" (while a NULL is boolean value "true").

Christian.

#11 lore raw
Quoted 6 lines · Christian Couder
Quoted 3 lines · Govind Salinas

Question. Wouldn't it reduce the amount of validation we have to do if whoever is calling back checked null and assigned an empty string? If so, we can probably replace all these patches with one patch.

Quoted 7 lines

This was discussed in this thread:

http://thread.gmane.org/gmane.comp.version-control.git/72107/

In short we can't do that now, but it may be possible latter, if we first deprecate using an empty string as boolean value "false" (while a NULL is boolean value "true").

It may be the case that many in-tree config parser functions are sloppy right now. It however is never an excuse to break user's existing repositories.

#12 lore raw
In reply to Govind Salinas
Quoted 4 lines · Govind Salinas

I think I got all the erroneous ones. I did

find . -name "*.c" | xargs grep git_config\( | awk '{ idx = index($2, ")"); p = substr($2, 12, idx - 12); print p }' | sort | uniq -u

It seems the "uniq -u" should be only "uniq". This way, you will also get the following ones to check:

git_default_config
git_diff_basic_config
git_log_config
git_pack_config

Thanks in advance,
Christian.

#13 lore raw
Quoted 6 lines · Christian Couder
Quoted 4 lines · Govind Salinas

I think I got all the erroneous ones. I did

find . -name "*.c" | xargs grep git_config\( | awk '{ idx = index($2, ")"); p = substr($2, 12, idx - 12); print p }' | sort | uniq -u

Quoted 7 lines

It seems the "uniq -u" should be only "uniq". This way, you will also get the following ones to check:

git_default_config
git_diff_basic_config
git_log_config
git_pack_config

I don't know awk so I cannot tell if there is something wrong with your script but with:

find . -name "*.c" | xargs perl -ne 'print "$1\n" if (m/git_config ? \(([^)]*)\)/)' | sort | uniq

I also get:

git_imap_config
show_all_config

Thanks,
Christian.

#14 lore raw
Quoted 18 lines · Christian Couder
Quoted 6 lines · Christian Couder
Quoted 4 lines · Govind Salinas

I think I got all the erroneous ones. I did

find . -name "*.c" | xargs grep git_config\( | awk '{ idx = index($2, ")"); p = substr($2, 12, idx - 12); print p }' | sort | uniq -u

Quoted 7 lines

It seems the "uniq -u" should be only "uniq". This way, you will also get the following ones to check:

git_default_config
git_diff_basic_config
git_log_config
git_pack_config

I don't know awk so I cannot tell if there is something wrong with your script but with:

Quoted 8 lines

find . -name "*.c" | xargs perl -ne 'print "$1\n" if (m/git_config ? \(([^)]*)\)/)' | sort | uniq

I also get:

git_imap_config
show_all_config

It appears only git_imap_config and git_default_config need patches. I will send them to the list.

Are any changes to the git_config_$type functions going to be made? It sounds like any change could break current configs so we are only going to stop the segfaults.

-Govind