summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormarkus@openbsd.org <markus@openbsd.org>2018-06-06 18:29:18 +0000
committerDamien Miller <djm@mindrot.org>2018-06-07 04:34:05 +1000
commit7f90635216851f6cb4bf3999e98b825f85d604f8 (patch)
treeac302db18a71c1e3c5d9077d1a820e37fbc2b9b5
parent392db2bc83215986a91c0b65feb0e40e7619ce7e (diff)
upstream: switch config file parsing to getline(3) as this avoids
static limits noted by gerhard@; ok dtucker@, djm@ OpenBSD-Commit-ID: 6d702eabef0fa12e5a1d75c334a8c8b325298b5c
-rw-r--r--auth2-pubkey.c16
-rw-r--r--authfile.c22
-rw-r--r--dh.c18
-rw-r--r--hostfile.c15
-rw-r--r--misc.c27
-rw-r--r--misc.h3
-rw-r--r--readconf.c10
-rw-r--r--servconf.c10
-rw-r--r--session.c11
-rw-r--r--ssh-keygen.c25
-rw-r--r--ssh-keyscan.c12
-rw-r--r--ssh.h9
12 files changed, 84 insertions, 94 deletions
diff --git a/auth2-pubkey.c b/auth2-pubkey.c
index 5603f5ef3..3ccc3a213 100644
--- a/auth2-pubkey.c
+++ b/auth2-pubkey.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: auth2-pubkey.c,v 1.78 2018/06/01 03:33:53 djm Exp $ */ 1/* $OpenBSD: auth2-pubkey.c,v 1.79 2018/06/06 18:29:18 markus Exp $ */
2/* 2/*
3 * Copyright (c) 2000 Markus Friedl. All rights reserved. 3 * Copyright (c) 2000 Markus Friedl. All rights reserved.
4 * 4 *
@@ -319,14 +319,16 @@ static int
319process_principals(struct ssh *ssh, FILE *f, const char *file, 319process_principals(struct ssh *ssh, FILE *f, const char *file,
320 const struct sshkey_cert *cert, struct sshauthopt **authoptsp) 320 const struct sshkey_cert *cert, struct sshauthopt **authoptsp)
321{ 321{
322 char loc[256], line[SSH_MAX_PUBKEY_BYTES], *cp, *ep; 322 char loc[256], *line = NULL, *cp, *ep;
323 size_t linesize = 0;
323 u_long linenum = 0; 324 u_long linenum = 0;
324 u_int found_principal = 0; 325 u_int found_principal = 0;
325 326
326 if (authoptsp != NULL) 327 if (authoptsp != NULL)
327 *authoptsp = NULL; 328 *authoptsp = NULL;
328 329
329 while (read_keyfile_line(f, file, line, sizeof(line), &linenum) != -1) { 330 while (getline(&line, &linesize, f) != -1) {
331 linenum++;
330 /* Always consume entire input */ 332 /* Always consume entire input */
331 if (found_principal) 333 if (found_principal)
332 continue; 334 continue;
@@ -344,6 +346,7 @@ process_principals(struct ssh *ssh, FILE *f, const char *file,
344 if (check_principals_line(ssh, cp, cert, loc, authoptsp) == 0) 346 if (check_principals_line(ssh, cp, cert, loc, authoptsp) == 0)
345 found_principal = 1; 347 found_principal = 1;
346 } 348 }
349 free(line);
347 return found_principal; 350 return found_principal;
348} 351}
349 352
@@ -687,14 +690,16 @@ static int
687check_authkeys_file(struct ssh *ssh, struct passwd *pw, FILE *f, 690check_authkeys_file(struct ssh *ssh, struct passwd *pw, FILE *f,
688 char *file, struct sshkey *key, struct sshauthopt **authoptsp) 691 char *file, struct sshkey *key, struct sshauthopt **authoptsp)
689{ 692{
690 char *cp, line[SSH_MAX_PUBKEY_BYTES], loc[256]; 693 char *cp, *line = NULL, loc[256];
694 size_t linesize = 0;
691 int found_key = 0; 695 int found_key = 0;
692 u_long linenum = 0; 696 u_long linenum = 0;
693 697
694 if (authoptsp != NULL) 698 if (authoptsp != NULL)
695 *authoptsp = NULL; 699 *authoptsp = NULL;
696 700
697 while (read_keyfile_line(f, file, line, sizeof(line), &linenum) != -1) { 701 while (getline(&line, &linesize, f) != -1) {
702 linenum++;
698 /* Always consume entire file */ 703 /* Always consume entire file */
699 if (found_key) 704 if (found_key)
700 continue; 705 continue;
@@ -708,6 +713,7 @@ check_authkeys_file(struct ssh *ssh, struct passwd *pw, FILE *f,
708 if (check_authkey_line(ssh, pw, key, cp, loc, authoptsp) == 0) 713 if (check_authkey_line(ssh, pw, key, cp, loc, authoptsp) == 0)
709 found_key = 1; 714 found_key = 1;
710 } 715 }
716 free(line);
711 return found_key; 717 return found_key;
712} 718}
713 719
diff --git a/authfile.c b/authfile.c
index 57dcd808c..c3a6345d3 100644
--- a/authfile.c
+++ b/authfile.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: authfile.c,v 1.128 2018/02/23 15:58:37 markus Exp $ */ 1/* $OpenBSD: authfile.c,v 1.129 2018/06/06 18:29:18 markus Exp $ */
2/* 2/*
3 * Copyright (c) 2000, 2013 Markus Friedl. All rights reserved. 3 * Copyright (c) 2000, 2013 Markus Friedl. All rights reserved.
4 * 4 *
@@ -265,17 +265,15 @@ static int
265sshkey_try_load_public(struct sshkey *k, const char *filename, char **commentp) 265sshkey_try_load_public(struct sshkey *k, const char *filename, char **commentp)
266{ 266{
267 FILE *f; 267 FILE *f;
268 char line[SSH_MAX_PUBKEY_BYTES]; 268 char *line = NULL, *cp;
269 char *cp; 269 size_t linesize = 0;
270 u_long linenum = 0;
271 int r; 270 int r;
272 271
273 if (commentp != NULL) 272 if (commentp != NULL)
274 *commentp = NULL; 273 *commentp = NULL;
275 if ((f = fopen(filename, "r")) == NULL) 274 if ((f = fopen(filename, "r")) == NULL)
276 return SSH_ERR_SYSTEM_ERROR; 275 return SSH_ERR_SYSTEM_ERROR;
277 while (read_keyfile_line(f, filename, line, sizeof(line), 276 while (getline(&line, &linesize, f) != -1) {
278 &linenum) != -1) {
279 cp = line; 277 cp = line;
280 switch (*cp) { 278 switch (*cp) {
281 case '#': 279 case '#':
@@ -299,11 +297,13 @@ sshkey_try_load_public(struct sshkey *k, const char *filename, char **commentp)
299 if (*commentp == NULL) 297 if (*commentp == NULL)
300 r = SSH_ERR_ALLOC_FAIL; 298 r = SSH_ERR_ALLOC_FAIL;
301 } 299 }
300 free(line);
302 fclose(f); 301 fclose(f);
303 return r; 302 return r;
304 } 303 }
305 } 304 }
306 } 305 }
306 free(line);
307 fclose(f); 307 fclose(f);
308 return SSH_ERR_INVALID_FORMAT; 308 return SSH_ERR_INVALID_FORMAT;
309} 309}
@@ -447,19 +447,18 @@ sshkey_in_file(struct sshkey *key, const char *filename, int strict_type,
447 int check_ca) 447 int check_ca)
448{ 448{
449 FILE *f; 449 FILE *f;
450 char line[SSH_MAX_PUBKEY_BYTES]; 450 char *line = NULL, *cp;
451 char *cp; 451 size_t linesize = 0;
452 u_long linenum = 0;
453 int r = 0; 452 int r = 0;
454 struct sshkey *pub = NULL; 453 struct sshkey *pub = NULL;
454
455 int (*sshkey_compare)(const struct sshkey *, const struct sshkey *) = 455 int (*sshkey_compare)(const struct sshkey *, const struct sshkey *) =
456 strict_type ? sshkey_equal : sshkey_equal_public; 456 strict_type ? sshkey_equal : sshkey_equal_public;
457 457
458 if ((f = fopen(filename, "r")) == NULL) 458 if ((f = fopen(filename, "r")) == NULL)
459 return SSH_ERR_SYSTEM_ERROR; 459 return SSH_ERR_SYSTEM_ERROR;
460 460
461 while (read_keyfile_line(f, filename, line, sizeof(line), 461 while (getline(&line, &linesize, f) != -1) {
462 &linenum) != -1) {
463 cp = line; 462 cp = line;
464 463
465 /* Skip leading whitespace. */ 464 /* Skip leading whitespace. */
@@ -491,6 +490,7 @@ sshkey_in_file(struct sshkey *key, const char *filename, int strict_type,
491 } 490 }
492 r = SSH_ERR_KEY_NOT_FOUND; 491 r = SSH_ERR_KEY_NOT_FOUND;
493 out: 492 out:
493 free(line);
494 sshkey_free(pub); 494 sshkey_free(pub);
495 fclose(f); 495 fclose(f);
496 return r; 496 return r;
diff --git a/dh.c b/dh.c
index 46afba033..16eb13276 100644
--- a/dh.c
+++ b/dh.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: dh.c,v 1.63 2018/02/07 02:06:50 jsing Exp $ */ 1/* $OpenBSD: dh.c,v 1.64 2018/06/06 18:29:18 markus Exp $ */
2/* 2/*
3 * Copyright (c) 2000 Niels Provos. All rights reserved. 3 * Copyright (c) 2000 Niels Provos. All rights reserved.
4 * 4 *
@@ -145,9 +145,9 @@ DH *
145choose_dh(int min, int wantbits, int max) 145choose_dh(int min, int wantbits, int max)
146{ 146{
147 FILE *f; 147 FILE *f;
148 char line[4096]; 148 char *line = NULL;
149 int best, bestcount, which; 149 size_t linesize = 0;
150 int linenum; 150 int best, bestcount, which, linenum;
151 struct dhgroup dhg; 151 struct dhgroup dhg;
152 152
153 if ((f = fopen(_PATH_DH_MODULI, "r")) == NULL) { 153 if ((f = fopen(_PATH_DH_MODULI, "r")) == NULL) {
@@ -158,7 +158,7 @@ choose_dh(int min, int wantbits, int max)
158 158
159 linenum = 0; 159 linenum = 0;
160 best = bestcount = 0; 160 best = bestcount = 0;
161 while (fgets(line, sizeof(line), f)) { 161 while (getline(&line, &linesize, f) != -1) {
162 linenum++; 162 linenum++;
163 if (!parse_prime(linenum, line, &dhg)) 163 if (!parse_prime(linenum, line, &dhg))
164 continue; 164 continue;
@@ -176,6 +176,9 @@ choose_dh(int min, int wantbits, int max)
176 if (dhg.size == best) 176 if (dhg.size == best)
177 bestcount++; 177 bestcount++;
178 } 178 }
179 free(line);
180 line = NULL;
181 linesize = 0;
179 rewind(f); 182 rewind(f);
180 183
181 if (bestcount == 0) { 184 if (bestcount == 0) {
@@ -186,7 +189,8 @@ choose_dh(int min, int wantbits, int max)
186 189
187 linenum = 0; 190 linenum = 0;
188 which = arc4random_uniform(bestcount); 191 which = arc4random_uniform(bestcount);
189 while (fgets(line, sizeof(line), f)) { 192 while (getline(&line, &linesize, f) != -1) {
193 linenum++;
190 if (!parse_prime(linenum, line, &dhg)) 194 if (!parse_prime(linenum, line, &dhg))
191 continue; 195 continue;
192 if ((dhg.size > max || dhg.size < min) || 196 if ((dhg.size > max || dhg.size < min) ||
@@ -198,6 +202,8 @@ choose_dh(int min, int wantbits, int max)
198 } 202 }
199 break; 203 break;
200 } 204 }
205 free(line);
206 line = NULL;
201 fclose(f); 207 fclose(f);
202 if (linenum != which+1) { 208 if (linenum != which+1) {
203 logit("WARNING: line %d disappeared in %s, giving up", 209 logit("WARNING: line %d disappeared in %s, giving up",
diff --git a/hostfile.c b/hostfile.c
index 12f174ff9..e08339379 100644
--- a/hostfile.c
+++ b/hostfile.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: hostfile.c,v 1.71 2017/05/31 09:15:42 deraadt Exp $ */ 1/* $OpenBSD: hostfile.c,v 1.72 2018/06/06 18:29:18 markus Exp $ */
2/* 2/*
3 * Author: Tatu Ylonen <ylo@cs.hut.fi> 3 * Author: Tatu Ylonen <ylo@cs.hut.fi>
4 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland 4 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
@@ -663,14 +663,14 @@ hostkeys_foreach(const char *path, hostkeys_foreach_fn *callback, void *ctx,
663 const char *host, const char *ip, u_int options) 663 const char *host, const char *ip, u_int options)
664{ 664{
665 FILE *f; 665 FILE *f;
666 char line[8192], oline[8192], ktype[128]; 666 char *line = NULL, ktype[128];
667 u_long linenum = 0; 667 u_long linenum = 0;
668 char *cp, *cp2; 668 char *cp, *cp2;
669 u_int kbits; 669 u_int kbits;
670 int hashed; 670 int hashed;
671 int s, r = 0; 671 int s, r = 0;
672 struct hostkey_foreach_line lineinfo; 672 struct hostkey_foreach_line lineinfo;
673 size_t l; 673 size_t linesize = 0, l;
674 674
675 memset(&lineinfo, 0, sizeof(lineinfo)); 675 memset(&lineinfo, 0, sizeof(lineinfo));
676 if (host == NULL && (options & HKF_WANT_MATCH) != 0) 676 if (host == NULL && (options & HKF_WANT_MATCH) != 0)
@@ -679,15 +679,16 @@ hostkeys_foreach(const char *path, hostkeys_foreach_fn *callback, void *ctx,
679 return SSH_ERR_SYSTEM_ERROR; 679 return SSH_ERR_SYSTEM_ERROR;
680 680
681 debug3("%s: reading file \"%s\"", __func__, path); 681 debug3("%s: reading file \"%s\"", __func__, path);
682 while (read_keyfile_line(f, path, line, sizeof(line), &linenum) == 0) { 682 while (getline(&line, &linesize, f) != -1) {
683 linenum++;
683 line[strcspn(line, "\n")] = '\0'; 684 line[strcspn(line, "\n")] = '\0';
684 strlcpy(oline, line, sizeof(oline));
685 685
686 sshkey_free(lineinfo.key); 686 sshkey_free(lineinfo.key);
687 memset(&lineinfo, 0, sizeof(lineinfo)); 687 memset(&lineinfo, 0, sizeof(lineinfo));
688 lineinfo.path = path; 688 lineinfo.path = path;
689 lineinfo.linenum = linenum; 689 lineinfo.linenum = linenum;
690 lineinfo.line = oline; 690 free(lineinfo.line);
691 lineinfo.line = xstrdup(line);
691 lineinfo.marker = MRK_NONE; 692 lineinfo.marker = MRK_NONE;
692 lineinfo.status = HKF_STATUS_OK; 693 lineinfo.status = HKF_STATUS_OK;
693 lineinfo.keytype = KEY_UNSPEC; 694 lineinfo.keytype = KEY_UNSPEC;
@@ -826,6 +827,8 @@ hostkeys_foreach(const char *path, hostkeys_foreach_fn *callback, void *ctx,
826 break; 827 break;
827 } 828 }
828 sshkey_free(lineinfo.key); 829 sshkey_free(lineinfo.key);
830 free(lineinfo.line);
831 free(line);
829 fclose(f); 832 fclose(f);
830 return r; 833 return r;
831} 834}
diff --git a/misc.c b/misc.c
index 874dcc8a2..3e62320ab 100644
--- a/misc.c
+++ b/misc.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: misc.c,v 1.127 2018/03/12 00:52:01 djm Exp $ */ 1/* $OpenBSD: misc.c,v 1.128 2018/06/06 18:29:18 markus Exp $ */
2/* 2/*
3 * Copyright (c) 2000 Markus Friedl. All rights reserved. 3 * Copyright (c) 2000 Markus Friedl. All rights reserved.
4 * Copyright (c) 2005,2006 Damien Miller. All rights reserved. 4 * Copyright (c) 2005,2006 Damien Miller. All rights reserved.
@@ -1005,31 +1005,6 @@ percent_expand(const char *string, ...)
1005#undef EXPAND_MAX_KEYS 1005#undef EXPAND_MAX_KEYS
1006} 1006}
1007 1007
1008/*
1009 * Read an entire line from a public key file into a static buffer, discarding
1010 * lines that exceed the buffer size. Returns 0 on success, -1 on failure.
1011 */
1012int
1013read_keyfile_line(FILE *f, const char *filename, char *buf, size_t bufsz,
1014 u_long *lineno)
1015{
1016 while (fgets(buf, bufsz, f) != NULL) {
1017 if (buf[0] == '\0')
1018 continue;
1019 (*lineno)++;
1020 if (buf[strlen(buf) - 1] == '\n' || feof(f)) {
1021 return 0;
1022 } else {
1023 debug("%s: %s line %lu exceeds size limit", __func__,
1024 filename, *lineno);
1025 /* discard remainder of line */
1026 while (fgetc(f) != '\n' && !feof(f))
1027 ; /* nothing */
1028 }
1029 }
1030 return -1;
1031}
1032
1033int 1008int
1034tun_open(int tun, int mode, char **ifname) 1009tun_open(int tun, int mode, char **ifname)
1035{ 1010{
diff --git a/misc.h b/misc.h
index cdafea735..daa4abc4c 100644
--- a/misc.h
+++ b/misc.h
@@ -1,4 +1,4 @@
1/* $OpenBSD: misc.h,v 1.71 2018/03/12 00:52:01 djm Exp $ */ 1/* $OpenBSD: misc.h,v 1.72 2018/06/06 18:29:18 markus Exp $ */
2 2
3/* 3/*
4 * Author: Tatu Ylonen <ylo@cs.hut.fi> 4 * Author: Tatu Ylonen <ylo@cs.hut.fi>
@@ -166,7 +166,6 @@ int safe_path_fd(int, const char *, struct passwd *,
166 166
167char *read_passphrase(const char *, int); 167char *read_passphrase(const char *, int);
168int ask_permission(const char *, ...) __attribute__((format(printf, 1, 2))); 168int ask_permission(const char *, ...) __attribute__((format(printf, 1, 2)));
169int read_keyfile_line(FILE *, const char *, char *, size_t, u_long *);
170 169
171#define MINIMUM(a, b) (((a) < (b)) ? (a) : (b)) 170#define MINIMUM(a, b) (((a) < (b)) ? (a) : (b))
172#define MAXIMUM(a, b) (((a) > (b)) ? (a) : (b)) 171#define MAXIMUM(a, b) (((a) > (b)) ? (a) : (b))
diff --git a/readconf.c b/readconf.c
index 9c4a234b5..733b67f76 100644
--- a/readconf.c
+++ b/readconf.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: readconf.c,v 1.288 2018/06/01 03:33:53 djm Exp $ */ 1/* $OpenBSD: readconf.c,v 1.289 2018/06/06 18:29:18 markus Exp $ */
2/* 2/*
3 * Author: Tatu Ylonen <ylo@cs.hut.fi> 3 * Author: Tatu Ylonen <ylo@cs.hut.fi>
4 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland 4 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
@@ -1728,7 +1728,8 @@ read_config_file_depth(const char *filename, struct passwd *pw,
1728 int flags, int *activep, int depth) 1728 int flags, int *activep, int depth)
1729{ 1729{
1730 FILE *f; 1730 FILE *f;
1731 char line[4096]; 1731 char *line = NULL;
1732 size_t linesize = 0;
1732 int linenum; 1733 int linenum;
1733 int bad_options = 0; 1734 int bad_options = 0;
1734 1735
@@ -1755,15 +1756,14 @@ read_config_file_depth(const char *filename, struct passwd *pw,
1755 * on/off by Host specifications. 1756 * on/off by Host specifications.
1756 */ 1757 */
1757 linenum = 0; 1758 linenum = 0;
1758 while (fgets(line, sizeof(line), f)) { 1759 while (getline(&line, &linesize, f) != -1) {
1759 /* Update line number counter. */ 1760 /* Update line number counter. */
1760 linenum++; 1761 linenum++;
1761 if (strlen(line) == sizeof(line) - 1)
1762 fatal("%s line %d too long", filename, linenum);
1763 if (process_config_line_depth(options, pw, host, original_host, 1762 if (process_config_line_depth(options, pw, host, original_host,
1764 line, filename, linenum, activep, flags, depth) != 0) 1763 line, filename, linenum, activep, flags, depth) != 0)
1765 bad_options++; 1764 bad_options++;
1766 } 1765 }
1766 free(line);
1767 fclose(f); 1767 fclose(f);
1768 if (bad_options > 0) 1768 if (bad_options > 0)
1769 fatal("%s: terminating, %d bad configuration options", 1769 fatal("%s: terminating, %d bad configuration options",
diff --git a/servconf.c b/servconf.c
index 3c41490b3..f55b66736 100644
--- a/servconf.c
+++ b/servconf.c
@@ -1,5 +1,5 @@
1 1
2/* $OpenBSD: servconf.c,v 1.330 2018/06/06 18:23:32 djm Exp $ */ 2/* $OpenBSD: servconf.c,v 1.331 2018/06/06 18:29:18 markus Exp $ */
3/* 3/*
4 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland 4 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved 5 * All rights reserved
@@ -2103,7 +2103,8 @@ process_server_config_line(ServerOptions *options, char *line,
2103void 2103void
2104load_server_config(const char *filename, Buffer *conf) 2104load_server_config(const char *filename, Buffer *conf)
2105{ 2105{
2106 char line[4096], *cp; 2106 char *line = NULL, *cp;
2107 size_t linesize = 0;
2107 FILE *f; 2108 FILE *f;
2108 int lineno = 0; 2109 int lineno = 0;
2109 2110
@@ -2113,10 +2114,8 @@ load_server_config(const char *filename, Buffer *conf)
2113 exit(1); 2114 exit(1);
2114 } 2115 }
2115 buffer_clear(conf); 2116 buffer_clear(conf);
2116 while (fgets(line, sizeof(line), f)) { 2117 while (getline(&line, &linesize, f) != -1) {
2117 lineno++; 2118 lineno++;
2118 if (strlen(line) == sizeof(line) - 1)
2119 fatal("%s line %d too long", filename, lineno);
2120 /* 2119 /*
2121 * Trim out comments and strip whitespace 2120 * Trim out comments and strip whitespace
2122 * NB - preserve newlines, they are needed to reproduce 2121 * NB - preserve newlines, they are needed to reproduce
@@ -2128,6 +2127,7 @@ load_server_config(const char *filename, Buffer *conf)
2128 2127
2129 buffer_append(conf, cp, strlen(cp)); 2128 buffer_append(conf, cp, strlen(cp));
2130 } 2129 }
2130 free(line);
2131 buffer_append(conf, "\0", 1); 2131 buffer_append(conf, "\0", 1);
2132 fclose(f); 2132 fclose(f);
2133 debug2("%s: done config len = %d", __func__, buffer_len(conf)); 2133 debug2("%s: done config len = %d", __func__, buffer_len(conf));
diff --git a/session.c b/session.c
index e72fcb0a8..511fc4e87 100644
--- a/session.c
+++ b/session.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: session.c,v 1.297 2018/06/06 18:23:32 djm Exp $ */ 1/* $OpenBSD: session.c,v 1.298 2018/06/06 18:29:18 markus Exp $ */
2/* 2/*
3 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland 3 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4 * All rights reserved 4 * All rights reserved
@@ -873,18 +873,18 @@ read_environment_file(char ***env, u_int *envsize,
873 const char *filename) 873 const char *filename)
874{ 874{
875 FILE *f; 875 FILE *f;
876 char buf[4096]; 876 char *line = NULL, *cp, *value;
877 char *cp, *value; 877 size_t linesize = 0;
878 u_int lineno = 0; 878 u_int lineno = 0;
879 879
880 f = fopen(filename, "r"); 880 f = fopen(filename, "r");
881 if (!f) 881 if (!f)
882 return; 882 return;
883 883
884 while (fgets(buf, sizeof(buf), f)) { 884 while (getline(&line, &linesize, f) != -1) {
885 if (++lineno > 1000) 885 if (++lineno > 1000)
886 fatal("Too many lines in environment file %s", filename); 886 fatal("Too many lines in environment file %s", filename);
887 for (cp = buf; *cp == ' ' || *cp == '\t'; cp++) 887 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
888 ; 888 ;
889 if (!*cp || *cp == '#' || *cp == '\n') 889 if (!*cp || *cp == '#' || *cp == '\n')
890 continue; 890 continue;
@@ -905,6 +905,7 @@ read_environment_file(char ***env, u_int *envsize,
905 value++; 905 value++;
906 child_set_env(env, envsize, cp, value); 906 child_set_env(env, envsize, cp, value);
907 } 907 }
908 free(line);
908 fclose(f); 909 fclose(f);
909} 910}
910 911
diff --git a/ssh-keygen.c b/ssh-keygen.c
index 2568c00e8..ccebbaf76 100644
--- a/ssh-keygen.c
+++ b/ssh-keygen.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: ssh-keygen.c,v 1.316 2018/06/01 04:21:29 djm Exp $ */ 1/* $OpenBSD: ssh-keygen.c,v 1.317 2018/06/06 18:29:18 markus Exp $ */
2/* 2/*
3 * Author: Tatu Ylonen <ylo@cs.hut.fi> 3 * Author: Tatu Ylonen <ylo@cs.hut.fi>
4 * Copyright (c) 1994 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland 4 * Copyright (c) 1994 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
@@ -870,7 +870,8 @@ do_fingerprint(struct passwd *pw)
870{ 870{
871 FILE *f; 871 FILE *f;
872 struct sshkey *public = NULL; 872 struct sshkey *public = NULL;
873 char *comment = NULL, *cp, *ep, line[SSH_MAX_PUBKEY_BYTES]; 873 char *comment = NULL, *cp, *ep, *line = NULL;
874 size_t linesize = 0;
874 int i, invalid = 1; 875 int i, invalid = 1;
875 const char *path; 876 const char *path;
876 u_long lnum = 0; 877 u_long lnum = 0;
@@ -885,7 +886,8 @@ do_fingerprint(struct passwd *pw)
885 } else if ((f = fopen(path, "r")) == NULL) 886 } else if ((f = fopen(path, "r")) == NULL)
886 fatal("%s: %s: %s", __progname, path, strerror(errno)); 887 fatal("%s: %s: %s", __progname, path, strerror(errno));
887 888
888 while (read_keyfile_line(f, path, line, sizeof(line), &lnum) == 0) { 889 while (getline(&line, &linesize, f) != -1) {
890 lnum++;
889 cp = line; 891 cp = line;
890 cp[strcspn(cp, "\n")] = '\0'; 892 cp[strcspn(cp, "\n")] = '\0';
891 /* Trim leading space and comments */ 893 /* Trim leading space and comments */
@@ -905,6 +907,7 @@ do_fingerprint(struct passwd *pw)
905 */ 907 */
906 if (lnum == 1 && strcmp(identity_file, "-") != 0 && 908 if (lnum == 1 && strcmp(identity_file, "-") != 0 &&
907 strstr(cp, "PRIVATE KEY") != NULL) { 909 strstr(cp, "PRIVATE KEY") != NULL) {
910 free(line);
908 fclose(f); 911 fclose(f);
909 fingerprint_private(path); 912 fingerprint_private(path);
910 exit(0); 913 exit(0);
@@ -951,6 +954,7 @@ do_fingerprint(struct passwd *pw)
951 invalid = 0; /* One good key in the file is sufficient */ 954 invalid = 0; /* One good key in the file is sufficient */
952 } 955 }
953 fclose(f); 956 fclose(f);
957 free(line);
954 958
955 if (invalid) 959 if (invalid)
956 fatal("%s is not a public key file.", path); 960 fatal("%s is not a public key file.", path);
@@ -2004,8 +2008,9 @@ do_show_cert(struct passwd *pw)
2004 struct stat st; 2008 struct stat st;
2005 int r, is_stdin = 0, ok = 0; 2009 int r, is_stdin = 0, ok = 0;
2006 FILE *f; 2010 FILE *f;
2007 char *cp, line[SSH_MAX_PUBKEY_BYTES]; 2011 char *cp, *line = NULL;
2008 const char *path; 2012 const char *path;
2013 size_t linesize = 0;
2009 u_long lnum = 0; 2014 u_long lnum = 0;
2010 2015
2011 if (!have_identity) 2016 if (!have_identity)
@@ -2021,7 +2026,8 @@ do_show_cert(struct passwd *pw)
2021 } else if ((f = fopen(identity_file, "r")) == NULL) 2026 } else if ((f = fopen(identity_file, "r")) == NULL)
2022 fatal("fopen %s: %s", identity_file, strerror(errno)); 2027 fatal("fopen %s: %s", identity_file, strerror(errno));
2023 2028
2024 while (read_keyfile_line(f, path, line, sizeof(line), &lnum) == 0) { 2029 while (getline(&line, &linesize, f) != -1) {
2030 lnum++;
2025 sshkey_free(key); 2031 sshkey_free(key);
2026 key = NULL; 2032 key = NULL;
2027 /* Trim leading space and comments */ 2033 /* Trim leading space and comments */
@@ -2046,6 +2052,7 @@ do_show_cert(struct passwd *pw)
2046 printf("%s:%lu:\n", path, lnum); 2052 printf("%s:%lu:\n", path, lnum);
2047 print_cert(key); 2053 print_cert(key);
2048 } 2054 }
2055 free(line);
2049 sshkey_free(key); 2056 sshkey_free(key);
2050 fclose(f); 2057 fclose(f);
2051 exit(ok ? 0 : 1); 2058 exit(ok ? 0 : 1);
@@ -2077,7 +2084,8 @@ update_krl_from_file(struct passwd *pw, const char *file, int wild_ca,
2077{ 2084{
2078 struct sshkey *key = NULL; 2085 struct sshkey *key = NULL;
2079 u_long lnum = 0; 2086 u_long lnum = 0;
2080 char *path, *cp, *ep, line[SSH_MAX_PUBKEY_BYTES]; 2087 char *path, *cp, *ep, *line = NULL;
2088 size_t linesize = 0;
2081 unsigned long long serial, serial2; 2089 unsigned long long serial, serial2;
2082 int i, was_explicit_key, was_sha1, r; 2090 int i, was_explicit_key, was_sha1, r;
2083 FILE *krl_spec; 2091 FILE *krl_spec;
@@ -2092,8 +2100,8 @@ update_krl_from_file(struct passwd *pw, const char *file, int wild_ca,
2092 2100
2093 if (!quiet) 2101 if (!quiet)
2094 printf("Revoking from %s\n", path); 2102 printf("Revoking from %s\n", path);
2095 while (read_keyfile_line(krl_spec, path, line, sizeof(line), 2103 while (getline(&line, &linesize, krl_spec) != -1) {
2096 &lnum) == 0) { 2104 lnum++;
2097 was_explicit_key = was_sha1 = 0; 2105 was_explicit_key = was_sha1 = 0;
2098 cp = line + strspn(line, " \t"); 2106 cp = line + strspn(line, " \t");
2099 /* Trim trailing space, comments and strip \n */ 2107 /* Trim trailing space, comments and strip \n */
@@ -2193,6 +2201,7 @@ update_krl_from_file(struct passwd *pw, const char *file, int wild_ca,
2193 } 2201 }
2194 if (strcmp(path, "-") != 0) 2202 if (strcmp(path, "-") != 0)
2195 fclose(krl_spec); 2203 fclose(krl_spec);
2204 free(line);
2196 free(path); 2205 free(path);
2197} 2206}
2198 2207
diff --git a/ssh-keyscan.c b/ssh-keyscan.c
index 381fb0844..38b1c548b 100644
--- a/ssh-keyscan.c
+++ b/ssh-keyscan.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: ssh-keyscan.c,v 1.119 2018/03/02 21:40:15 jmc Exp $ */ 1/* $OpenBSD: ssh-keyscan.c,v 1.120 2018/06/06 18:29:18 markus Exp $ */
2/* 2/*
3 * Copyright 1995, 1996 by David Mazieres <dm@lcs.mit.edu>. 3 * Copyright 1995, 1996 by David Mazieres <dm@lcs.mit.edu>.
4 * 4 *
@@ -646,9 +646,9 @@ main(int argc, char **argv)
646{ 646{
647 int debug_flag = 0, log_level = SYSLOG_LEVEL_INFO; 647 int debug_flag = 0, log_level = SYSLOG_LEVEL_INFO;
648 int opt, fopt_count = 0, j; 648 int opt, fopt_count = 0, j;
649 char *tname, *cp, line[NI_MAXHOST]; 649 char *tname, *cp, *line = NULL;
650 size_t linesize = 0;
650 FILE *fp; 651 FILE *fp;
651 u_long linenum;
652 652
653 extern int optind; 653 extern int optind;
654 extern char *optarg; 654 extern char *optarg;
@@ -769,11 +769,8 @@ main(int argc, char **argv)
769 else if ((fp = fopen(argv[j], "r")) == NULL) 769 else if ((fp = fopen(argv[j], "r")) == NULL)
770 fatal("%s: %s: %s", __progname, argv[j], 770 fatal("%s: %s: %s", __progname, argv[j],
771 strerror(errno)); 771 strerror(errno));
772 linenum = 0;
773 772
774 while (read_keyfile_line(fp, 773 while (getline(&line, &linesize, fp) != -1) {
775 argv[j] == NULL ? "(stdin)" : argv[j], line, sizeof(line),
776 &linenum) != -1) {
777 /* Chomp off trailing whitespace and comments */ 774 /* Chomp off trailing whitespace and comments */
778 if ((cp = strchr(line, '#')) == NULL) 775 if ((cp = strchr(line, '#')) == NULL)
779 cp = line + strlen(line) - 1; 776 cp = line + strlen(line) - 1;
@@ -798,6 +795,7 @@ main(int argc, char **argv)
798 795
799 fclose(fp); 796 fclose(fp);
800 } 797 }
798 free(line);
801 799
802 while (optind < argc) 800 while (optind < argc)
803 do_host(argv[optind++]); 801 do_host(argv[optind++]);
diff --git a/ssh.h b/ssh.h
index 12d800922..5abfd7a68 100644
--- a/ssh.h
+++ b/ssh.h
@@ -1,4 +1,4 @@
1/* $OpenBSD: ssh.h,v 1.87 2017/05/07 23:15:59 djm Exp $ */ 1/* $OpenBSD: ssh.h,v 1.88 2018/06/06 18:29:18 markus Exp $ */
2 2
3/* 3/*
4 * Author: Tatu Ylonen <ylo@cs.hut.fi> 4 * Author: Tatu Ylonen <ylo@cs.hut.fi>
@@ -31,13 +31,6 @@
31#define SSH_MAX_IDENTITY_FILES 100 31#define SSH_MAX_IDENTITY_FILES 100
32 32
33/* 33/*
34 * Maximum length of lines in authorized_keys file.
35 * Current value permits 16kbit RSA keys and 8kbit DSA keys, with
36 * some room for options and comments.
37 */
38#define SSH_MAX_PUBKEY_BYTES 16384
39
40/*
41 * Major protocol version. Different version indicates major incompatibility 34 * Major protocol version. Different version indicates major incompatibility
42 * that prevents communication. 35 * that prevents communication.
43 * 36 *