=================================================================== RCS file: /cvsrepo/anoncvs/cvs/src/usr.bin/ssh/authfile.c,v retrieving revision 1.136 retrieving revision 1.137 diff -u -r1.136 -r1.137 --- src/usr.bin/ssh/authfile.c 2020/01/02 22:38:33 1.136 +++ src/usr.bin/ssh/authfile.c 2020/01/25 23:02:13 1.137 @@ -1,4 +1,4 @@ -/* $OpenBSD: authfile.c,v 1.136 2020/01/02 22:38:33 djm Exp $ */ +/* $OpenBSD: authfile.c,v 1.137 2020/01/25 23:02:13 djm Exp $ */ /* * Copyright (c) 2000, 2013 Markus Friedl. All rights reserved. * @@ -53,20 +53,13 @@ static int sshkey_save_private_blob(struct sshbuf *keybuf, const char *filename) { - int fd, oerrno; + int r; + mode_t omask; - if ((fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0600)) == -1) - return SSH_ERR_SYSTEM_ERROR; - if (atomicio(vwrite, fd, sshbuf_mutable_ptr(keybuf), - sshbuf_len(keybuf)) != sshbuf_len(keybuf)) { - oerrno = errno; - close(fd); - unlink(filename); - errno = oerrno; - return SSH_ERR_SYSTEM_ERROR; - } - close(fd); - return 0; + omask = umask(077); + r = sshbuf_write_file(filename, keybuf); + umask(omask); + return r; } int @@ -90,49 +83,6 @@ return r; } -/* Load a key from a fd into a buffer */ -int -sshkey_load_file(int fd, struct sshbuf *blob) -{ - u_char buf[1024]; - size_t len; - struct stat st; - int r; - - if (fstat(fd, &st) == -1) - return SSH_ERR_SYSTEM_ERROR; - if ((st.st_mode & (S_IFSOCK|S_IFCHR|S_IFIFO)) == 0 && - st.st_size > MAX_KEY_FILE_SIZE) - return SSH_ERR_INVALID_FORMAT; - for (;;) { - if ((len = atomicio(read, fd, buf, sizeof(buf))) == 0) { - if (errno == EPIPE) - break; - r = SSH_ERR_SYSTEM_ERROR; - goto out; - } - if ((r = sshbuf_put(blob, buf, len)) != 0) - goto out; - if (sshbuf_len(blob) > MAX_KEY_FILE_SIZE) { - r = SSH_ERR_INVALID_FORMAT; - goto out; - } - } - if ((st.st_mode & (S_IFSOCK|S_IFCHR|S_IFIFO)) == 0 && - st.st_size != (off_t)sshbuf_len(blob)) { - r = SSH_ERR_FILE_CHANGED; - goto out; - } - r = 0; - - out: - explicit_bzero(buf, sizeof(buf)); - if (r != 0) - sshbuf_reset(blob); - return r; -} - - /* XXX remove error() calls from here? */ int sshkey_perm_ok(int fd, const char *filename) @@ -194,11 +144,7 @@ if (keyp != NULL) *keyp = NULL; - if ((buffer = sshbuf_new()) == NULL) { - r = SSH_ERR_ALLOC_FAIL; - goto out; - } - if ((r = sshkey_load_file(fd, buffer)) != 0 || + if ((r = sshbuf_load_fd(fd, &buffer)) != 0 || (r = sshkey_parse_private_fileblob_type(buffer, type, passphrase, keyp, commentp)) != 0) goto out; @@ -229,12 +175,7 @@ r = SSH_ERR_KEY_BAD_PERMISSIONS; goto out; } - - if ((buffer = sshbuf_new()) == NULL) { - r = SSH_ERR_ALLOC_FAIL; - goto out; - } - if ((r = sshkey_load_file(fd, buffer)) != 0 || + if ((r = sshbuf_load_fd(fd, &buffer)) != 0 || (r = sshkey_parse_private_fileblob(buffer, passphrase, keyp, commentp)) != 0) goto out;