summaryrefslogtreecommitdiff
path: root/libexec/tftpd
diff options
context:
space:
mode:
authorAlan Somers <asomers@FreeBSD.org>2018-07-22 16:14:30 +0000
committerAlan Somers <asomers@FreeBSD.org>2018-07-22 16:14:30 +0000
commit3c0fa265346ec04fc8277fba9c3fe28d1e26068a (patch)
tree3b1a19664ad53961d92a5995170e2f877111cc7d /libexec/tftpd
parent12395dc9f6bfd1e40ac41ef8fb6af966ad647a2e (diff)
downloadsrc-test-3c0fa265346ec04fc8277fba9c3fe28d1e26068a.tar.gz
src-test-3c0fa265346ec04fc8277fba9c3fe28d1e26068a.zip
Fix multiple Coverity warnings in tftpd(8)
* Initialize uninitialized variable (CID 1006502) * strcpy => strlcpy (CID 1006792, 1006791, 1006790) * Check function return values (CID 1009442, 1009441, 1009440) * Delete dead code in receive_packet (not reported by Coverity) * Remove redundant alarm(3) in receive_packet (not reported by Coverity) Reported by: Coverity CID: 1006502, 1006792, 1006791, 1006790, 1009442, 1009441, 1009440 MFC after: 2 weeks Differential Revision: https://reviews.freebsd.org/D11287
Notes
Notes: svn path=/head/; revision=336605
Diffstat (limited to 'libexec/tftpd')
-rw-r--r--libexec/tftpd/tftp-file.c4
-rw-r--r--libexec/tftpd/tftp-io.c16
-rw-r--r--libexec/tftpd/tftp-utils.c4
-rw-r--r--libexec/tftpd/tftpd.c10
4 files changed, 17 insertions, 17 deletions
diff --git a/libexec/tftpd/tftp-file.c b/libexec/tftpd/tftp-file.c
index 84ef2d7ee19e5..3e06f5df6edb2 100644
--- a/libexec/tftpd/tftp-file.c
+++ b/libexec/tftpd/tftp-file.c
@@ -36,6 +36,7 @@ __FBSDID("$FreeBSD$");
#include <netinet/in.h>
#include <arpa/tftp.h>
+#include <assert.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
@@ -80,7 +81,8 @@ convert_from_net(char *buffer, size_t count)
if (buffer[i] == '\n') {
if (n == 0) {
if (ftell(file) != 0) {
- fseek(file, -1, SEEK_END);
+ int r = fseek(file, -1, SEEK_END);
+ assert(r == 0);
convbuffer[n++] = '\n';
} else {
/* This shouldn't happen */
diff --git a/libexec/tftpd/tftp-io.c b/libexec/tftpd/tftp-io.c
index 06d9522f16af7..192097b5bf542 100644
--- a/libexec/tftpd/tftp-io.c
+++ b/libexec/tftpd/tftp-io.c
@@ -36,6 +36,7 @@ __FBSDID("$FreeBSD$");
#include <arpa/tftp.h>
#include <arpa/inet.h>
+#include <assert.h>
#include <errno.h>
#include <setjmp.h>
#include <signal.h>
@@ -395,7 +396,7 @@ receive_packet(int peer, char *data, int size, struct sockaddr_storage *from,
struct sockaddr_storage *pfrom;
socklen_t fromlen;
int n;
- static int waiting;
+ static int timed_out;
if (debug&DEBUG_PACKETS)
tftp_log(LOG_DEBUG,
@@ -403,23 +404,16 @@ receive_packet(int peer, char *data, int size, struct sockaddr_storage *from,
pkt = (struct tftphdr *)data;
- waiting = 0;
signal(SIGALRM, timeout);
- setjmp(timeoutbuf);
+ timed_out = setjmp(timeoutbuf);
alarm(thistimeout);
- if (waiting > 0) {
- alarm(0);
- return (RP_TIMEOUT);
- }
-
- if (waiting > 0) {
+ if (timed_out != 0) {
tftp_log(LOG_ERR, "receive_packet: timeout");
alarm(0);
return (RP_TIMEOUT);
}
- waiting++;
pfrom = (from == NULL) ? &from_local : from;
fromlen = sizeof(*pfrom);
n = recvfrom(peer, data, size, 0, (struct sockaddr *)pfrom, &fromlen);
@@ -433,8 +427,6 @@ receive_packet(int peer, char *data, int size, struct sockaddr_storage *from,
return (RP_TIMEOUT);
}
- alarm(0);
-
if (n < 0) {
/* No idea what could have happened if it isn't a timeout */
tftp_log(LOG_ERR, "receive_packet: %s", strerror(errno));
diff --git a/libexec/tftpd/tftp-utils.c b/libexec/tftpd/tftp-utils.c
index f578a7e6d831e..f9f8f4dc922a2 100644
--- a/libexec/tftpd/tftp-utils.c
+++ b/libexec/tftpd/tftp-utils.c
@@ -270,11 +270,13 @@ char *
rp_strerror(int error)
{
static char s[100];
+ size_t space = sizeof(s);
int i = 0;
while (rp_errors[i].desc != NULL) {
if (rp_errors[i].error == error) {
- strcpy(s, rp_errors[i].desc);
+ strlcpy(s, rp_errors[i].desc, space);
+ space -= strlen(rp_errors[i].desc);
}
i++;
}
diff --git a/libexec/tftpd/tftpd.c b/libexec/tftpd/tftpd.c
index 976a58a0ef587..6d09ad73befaa 100644
--- a/libexec/tftpd/tftpd.c
+++ b/libexec/tftpd/tftpd.c
@@ -374,7 +374,10 @@ main(int argc, char *argv[])
exit(1);
}
chdir("/");
- setgroups(1, &nobody->pw_gid);
+ if (setgroups(1, &nobody->pw_gid) != 0) {
+ tftp_log(LOG_ERR, "setgroups failed");
+ exit(1);
+ }
if (setuid(nobody->pw_uid) != 0) {
tftp_log(LOG_ERR, "setuid failed");
exit(1);
@@ -522,7 +525,7 @@ tftp_wrq(int peer, char *recvbuffer, ssize_t size)
cp = parse_header(peer, recvbuffer, size, &filename, &mode);
size -= (cp - recvbuffer) + 1;
- strcpy(fnbuf, filename);
+ strlcpy(fnbuf, filename, sizeof(fnbuf));
reduce_path(fnbuf);
filename = fnbuf;
@@ -567,7 +570,7 @@ tftp_rrq(int peer, char *recvbuffer, ssize_t size)
cp = parse_header(peer, recvbuffer, size, &filename, &mode);
size -= (cp - recvbuffer) + 1;
- strcpy(fnbuf, filename);
+ strlcpy(fnbuf, filename, sizeof(fnbuf));
reduce_path(fnbuf);
filename = fnbuf;
@@ -804,6 +807,7 @@ tftp_xmitfile(int peer, const char *mode)
time_t now;
struct tftp_stats ts;
+ memset(&ts, 0, sizeof(ts));
now = time(NULL);
if (debug&DEBUG_SIMPLE)
tftp_log(LOG_DEBUG, "Transmitting file");