diff options
author | millert <> | 2004-11-30 15:12:59 +0000 |
---|---|---|
committer | millert <> | 2004-11-30 15:12:59 +0000 |
commit | 0763d6b5127f254e776c4d39ecbeca9d455f1399 (patch) | |
tree | e52822776050d5a70590bbc46904aa8b7ae3a65e | |
parent | 21edc335077ec883a751979cb9a75e07da6677a0 (diff) | |
download | openbsd-0763d6b5127f254e776c4d39ecbeca9d455f1399.tar.gz openbsd-0763d6b5127f254e776c4d39ecbeca9d455f1399.tar.bz2 openbsd-0763d6b5127f254e776c4d39ecbeca9d455f1399.zip |
Check strlc{py,at} return value and return NULL upon truncation instead
of silently truncating. OK deraadt@ otto@
-rw-r--r-- | src/lib/libc/stdlib/realpath.c | 33 |
1 files changed, 23 insertions, 10 deletions
diff --git a/src/lib/libc/stdlib/realpath.c b/src/lib/libc/stdlib/realpath.c index 1525d0372f..37b4ad6159 100644 --- a/src/lib/libc/stdlib/realpath.c +++ b/src/lib/libc/stdlib/realpath.c | |||
@@ -31,7 +31,7 @@ | |||
31 | */ | 31 | */ |
32 | 32 | ||
33 | #if defined(LIBC_SCCS) && !defined(lint) | 33 | #if defined(LIBC_SCCS) && !defined(lint) |
34 | static char *rcsid = "$OpenBSD: realpath.c,v 1.10 2003/08/01 21:04:59 millert Exp $"; | 34 | static char *rcsid = "$OpenBSD: realpath.c,v 1.11 2004/11/30 15:12:59 millert Exp $"; |
35 | #endif /* LIBC_SCCS and not lint */ | 35 | #endif /* LIBC_SCCS and not lint */ |
36 | 36 | ||
37 | #include <sys/param.h> | 37 | #include <sys/param.h> |
@@ -62,7 +62,8 @@ realpath(path, resolved) | |||
62 | 62 | ||
63 | /* Save the starting point. */ | 63 | /* Save the starting point. */ |
64 | if ((fd = open(".", O_RDONLY)) < 0) { | 64 | if ((fd = open(".", O_RDONLY)) < 0) { |
65 | (void)strlcpy(resolved, ".", MAXPATHLEN); | 65 | resolved[0] = '.'; |
66 | resolved[1] = '\0'; | ||
66 | return (NULL); | 67 | return (NULL); |
67 | } | 68 | } |
68 | 69 | ||
@@ -78,7 +79,10 @@ realpath(path, resolved) | |||
78 | * if it is a directory, then change to that directory. | 79 | * if it is a directory, then change to that directory. |
79 | * get the current directory name and append the basename. | 80 | * get the current directory name and append the basename. |
80 | */ | 81 | */ |
81 | strlcpy(resolved, path, MAXPATHLEN); | 82 | if (strlcpy(resolved, path, MAXPATHLEN) >= MAXPATHLEN) { |
83 | serrno = ENAMETOOLONG; | ||
84 | goto err2; | ||
85 | } | ||
82 | loop: | 86 | loop: |
83 | q = strrchr(resolved, '/'); | 87 | q = strrchr(resolved, '/'); |
84 | if (q != NULL) { | 88 | if (q != NULL) { |
@@ -104,8 +108,7 @@ loop: | |||
104 | errno = ELOOP; | 108 | errno = ELOOP; |
105 | goto err1; | 109 | goto err1; |
106 | } | 110 | } |
107 | n = readlink(p, resolved, MAXPATHLEN-1); | 111 | if ((n = readlink(p, resolved, MAXPATHLEN-1)) < 0) |
108 | if (n < 0) | ||
109 | goto err1; | 112 | goto err1; |
110 | resolved[n] = '\0'; | 113 | resolved[n] = '\0'; |
111 | goto loop; | 114 | goto loop; |
@@ -121,8 +124,11 @@ loop: | |||
121 | * Save the last component name and get the full pathname of | 124 | * Save the last component name and get the full pathname of |
122 | * the current directory. | 125 | * the current directory. |
123 | */ | 126 | */ |
124 | (void)strlcpy(wbuf, p, sizeof wbuf); | 127 | if (strlcpy(wbuf, p, sizeof(wbuf)) >= sizeof(wbuf)) { |
125 | if (getcwd(resolved, MAXPATHLEN) == 0) | 128 | errno = ENAMETOOLONG; |
129 | goto err1; | ||
130 | } | ||
131 | if (getcwd(resolved, MAXPATHLEN) == NULL) | ||
126 | goto err1; | 132 | goto err1; |
127 | 133 | ||
128 | /* | 134 | /* |
@@ -139,9 +145,16 @@ loop: | |||
139 | errno = ENAMETOOLONG; | 145 | errno = ENAMETOOLONG; |
140 | goto err1; | 146 | goto err1; |
141 | } | 147 | } |
142 | if (needslash) | 148 | if (needslash) { |
143 | strlcat(resolved, "/", MAXPATHLEN); | 149 | if (strlcat(resolved, "/", MAXPATHLEN) >= MAXPATHLEN) { |
144 | strlcat(resolved, wbuf, MAXPATHLEN); | 150 | errno = ENAMETOOLONG; |
151 | goto err1; | ||
152 | } | ||
153 | } | ||
154 | if (strlcat(resolved, wbuf, MAXPATHLEN) >= MAXPATHLEN) { | ||
155 | errno = ENAMETOOLONG; | ||
156 | goto err1; | ||
157 | } | ||
145 | } | 158 | } |
146 | 159 | ||
147 | /* Go back to where we came from. */ | 160 | /* Go back to where we came from. */ |