execv_git_cmd: Fix stack buffer overflow.
Signed-off-by: Dmitry V. Levin <ldv@altlinux.org> Signed-off-by: Junio C Hamano <junkio@cox.net>
Dmitry V. Levin committed
May 30, 2006 at 18:58 UTC
d6859901012535b3781e5332923ede51d5977395
1 file changed
+23
-9
exec_cmd.c
+23
-9
@@ -32,12 +32,14 @@ const char *git_exec_path(void)
32
int execv_git_cmd(const char **argv)
33
{
34
char git_command[PATH_MAX + 1];
35
- int len, i;
35
+ int i;
36
const char *paths[] = { current_exec_path,
37
getenv("GIT_EXEC_PATH"),
38
builtin_exec_path };
39
40
for (i = 0; i < ARRAY_SIZE(paths); ++i) {
41
+ size_t len;
42
+ int rc;
43
const char *exec_dir = paths[i];
44
const char *tmp;
45
@@ -46,8 +48,9 @@ int execv_git_cmd(const char **argv)
48
if (*exec_dir != '/') {
49
if (!getcwd(git_command, sizeof(git_command))) {
50
fprintf(stderr, "git: cannot determine "
49
- "current directory\n");
50
- exit(1);
51
+ "current directory: %s\n",
52
+ strerror(errno));
53
+ break;
54
}
55
len = strlen(git_command);
56
@@ -57,17 +60,28 @@ int execv_git_cmd(const char **argv)
60
while (*exec_dir == '/')
61
exec_dir++;
62
}
60
- snprintf(git_command + len, sizeof(git_command) - len,
61
- "/%s", exec_dir);
63
+
64
+ rc = snprintf(git_command + len,
65
+ sizeof(git_command) - len, "/%s",
66
+ exec_dir);
67
+ if (rc < 0 || rc >= sizeof(git_command) - len) {
68
+ fprintf(stderr, "git: command name given "
69
+ "is too long.\n");
70
+ break;
71
+ }
72
} else {
73
+ if (strlen(exec_dir) + 1 > sizeof(git_command)) {
74
+ fprintf(stderr, "git: command name given "
75
+ "is too long.\n");
76
+ break;
77
+ }
78
strcpy(git_command, exec_dir);
79
}
80
81
len = strlen(git_command);
67
- len += snprintf(git_command + len, sizeof(git_command) - len,
68
- "/git-%s", argv[0]);
69
-
70
- if (sizeof(git_command) <= len) {
82
+ rc = snprintf(git_command + len, sizeof(git_command) - len,
83
+ "/git-%s", argv[0]);
84
+ if (rc < 0 || rc >= sizeof(git_command) - len) {
85
fprintf(stderr,
86
"git: command name given is too long.\n");
87
break;