xref: /freebsd/crypto/openssh/sshbuf-io.c (revision 19261079)
1*19261079SEd Maste /*	$OpenBSD: sshbuf-io.c,v 1.2 2020/01/25 23:28:06 djm Exp $ */
2*19261079SEd Maste /*
3*19261079SEd Maste  * Copyright (c) 2011 Damien Miller
4*19261079SEd Maste  *
5*19261079SEd Maste  * Permission to use, copy, modify, and distribute this software for any
6*19261079SEd Maste  * purpose with or without fee is hereby granted, provided that the above
7*19261079SEd Maste  * copyright notice and this permission notice appear in all copies.
8*19261079SEd Maste  *
9*19261079SEd Maste  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10*19261079SEd Maste  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11*19261079SEd Maste  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12*19261079SEd Maste  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13*19261079SEd Maste  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14*19261079SEd Maste  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15*19261079SEd Maste  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16*19261079SEd Maste  */
17*19261079SEd Maste 
18*19261079SEd Maste #include "includes.h"
19*19261079SEd Maste 
20*19261079SEd Maste #include <sys/types.h>
21*19261079SEd Maste #include <sys/stat.h>
22*19261079SEd Maste 
23*19261079SEd Maste #include <errno.h>
24*19261079SEd Maste #include <fcntl.h>
25*19261079SEd Maste #include <unistd.h>
26*19261079SEd Maste #include <string.h>
27*19261079SEd Maste 
28*19261079SEd Maste #include "ssherr.h"
29*19261079SEd Maste #include "sshbuf.h"
30*19261079SEd Maste #include "atomicio.h"
31*19261079SEd Maste 
32*19261079SEd Maste /* Load a file from a fd into a buffer */
33*19261079SEd Maste int
sshbuf_load_fd(int fd,struct sshbuf ** blobp)34*19261079SEd Maste sshbuf_load_fd(int fd, struct sshbuf **blobp)
35*19261079SEd Maste {
36*19261079SEd Maste 	u_char buf[4096];
37*19261079SEd Maste 	size_t len;
38*19261079SEd Maste 	struct stat st;
39*19261079SEd Maste 	int r;
40*19261079SEd Maste 	struct sshbuf *blob;
41*19261079SEd Maste 
42*19261079SEd Maste 	*blobp = NULL;
43*19261079SEd Maste 
44*19261079SEd Maste 	if (fstat(fd, &st) == -1)
45*19261079SEd Maste 		return SSH_ERR_SYSTEM_ERROR;
46*19261079SEd Maste 	if ((st.st_mode & (S_IFSOCK|S_IFCHR|S_IFIFO)) == 0 &&
47*19261079SEd Maste 	    st.st_size > SSHBUF_SIZE_MAX)
48*19261079SEd Maste 		return SSH_ERR_INVALID_FORMAT;
49*19261079SEd Maste 	if ((blob = sshbuf_new()) == NULL)
50*19261079SEd Maste 		return SSH_ERR_ALLOC_FAIL;
51*19261079SEd Maste 	for (;;) {
52*19261079SEd Maste 		if ((len = atomicio(read, fd, buf, sizeof(buf))) == 0) {
53*19261079SEd Maste 			if (errno == EPIPE)
54*19261079SEd Maste 				break;
55*19261079SEd Maste 			r = SSH_ERR_SYSTEM_ERROR;
56*19261079SEd Maste 			goto out;
57*19261079SEd Maste 		}
58*19261079SEd Maste 		if ((r = sshbuf_put(blob, buf, len)) != 0)
59*19261079SEd Maste 			goto out;
60*19261079SEd Maste 		if (sshbuf_len(blob) > SSHBUF_SIZE_MAX) {
61*19261079SEd Maste 			r = SSH_ERR_INVALID_FORMAT;
62*19261079SEd Maste 			goto out;
63*19261079SEd Maste 		}
64*19261079SEd Maste 	}
65*19261079SEd Maste 	if ((st.st_mode & (S_IFSOCK|S_IFCHR|S_IFIFO)) == 0 &&
66*19261079SEd Maste 	    st.st_size != (off_t)sshbuf_len(blob)) {
67*19261079SEd Maste 		r = SSH_ERR_FILE_CHANGED;
68*19261079SEd Maste 		goto out;
69*19261079SEd Maste 	}
70*19261079SEd Maste 	/* success */
71*19261079SEd Maste 	*blobp = blob;
72*19261079SEd Maste 	blob = NULL; /* transferred */
73*19261079SEd Maste 	r = 0;
74*19261079SEd Maste  out:
75*19261079SEd Maste 	explicit_bzero(buf, sizeof(buf));
76*19261079SEd Maste 	sshbuf_free(blob);
77*19261079SEd Maste 	return r;
78*19261079SEd Maste }
79*19261079SEd Maste 
80*19261079SEd Maste int
sshbuf_load_file(const char * path,struct sshbuf ** bufp)81*19261079SEd Maste sshbuf_load_file(const char *path, struct sshbuf **bufp)
82*19261079SEd Maste {
83*19261079SEd Maste 	int r, fd, oerrno;
84*19261079SEd Maste 
85*19261079SEd Maste 	*bufp = NULL;
86*19261079SEd Maste 	if ((fd = open(path, O_RDONLY)) == -1)
87*19261079SEd Maste 		return SSH_ERR_SYSTEM_ERROR;
88*19261079SEd Maste 	if ((r = sshbuf_load_fd(fd, bufp)) != 0)
89*19261079SEd Maste 		goto out;
90*19261079SEd Maste 	/* success */
91*19261079SEd Maste 	r = 0;
92*19261079SEd Maste  out:
93*19261079SEd Maste 	oerrno = errno;
94*19261079SEd Maste 	close(fd);
95*19261079SEd Maste 	if (r != 0)
96*19261079SEd Maste 		errno = oerrno;
97*19261079SEd Maste 	return r;
98*19261079SEd Maste }
99*19261079SEd Maste 
100*19261079SEd Maste int
sshbuf_write_file(const char * path,struct sshbuf * buf)101*19261079SEd Maste sshbuf_write_file(const char *path, struct sshbuf *buf)
102*19261079SEd Maste {
103*19261079SEd Maste 	int fd, oerrno;
104*19261079SEd Maste 
105*19261079SEd Maste 	if ((fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644)) == -1)
106*19261079SEd Maste 		return SSH_ERR_SYSTEM_ERROR;
107*19261079SEd Maste 	if (atomicio(vwrite, fd, sshbuf_mutable_ptr(buf),
108*19261079SEd Maste 	    sshbuf_len(buf)) != sshbuf_len(buf) || close(fd) != 0) {
109*19261079SEd Maste 		oerrno = errno;
110*19261079SEd Maste 		close(fd);
111*19261079SEd Maste 		unlink(path);
112*19261079SEd Maste 		errno = oerrno;
113*19261079SEd Maste 		return SSH_ERR_SYSTEM_ERROR;
114*19261079SEd Maste 	}
115*19261079SEd Maste 	return 0;
116*19261079SEd Maste }
117*19261079SEd Maste 
118