Skip to content
This repository was archived by the owner on Jan 6, 2023. It is now read-only.

Commit 7733ce6

Browse files
author
Sebastien Boeuf
committed
security: Retrieve the number of bytes read
Some calls to read() were ignoring the number of bytes actually read by the function. Signed-off-by: Sebastien Boeuf <[email protected]>
1 parent febdce8 commit 7733ce6

File tree

3 files changed

+23
-4
lines changed

3 files changed

+23
-4
lines changed

src/init.c

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ struct hyper_pod global_pod = {
3838

3939
#define MAXEVENTS 10
4040

41+
#define PROC_UPTIME_PATH "/proc/uptime"
42+
4143
struct hyper_ctl ctl;
4244

4345
sigset_t orig_mask;
@@ -517,7 +519,8 @@ static int hyper_setup_pod(struct hyper_pod *pod)
517519
static void hyper_print_uptime(void)
518520
{
519521
char buf[128];
520-
int fd = open("/proc/uptime", O_RDONLY);
522+
int fd = open(PROC_UPTIME_PATH, O_RDONLY);
523+
ssize_t bytes_read;
521524

522525
if (fd < 0)
523526
return;
@@ -526,8 +529,20 @@ static void hyper_print_uptime(void)
526529
memset(buf, 0, buffer_size + 1);
527530
buf[buffer_size] = '\0';
528531

529-
if (read(fd, buf, buffer_size))
532+
bytes_read = read(fd, buf, buffer_size);
533+
if (bytes_read < 0) {
534+
fprintf(stdout, "reading %s failed: %s\n",
535+
PROC_UPTIME_PATH, strerror(errno));
536+
} else if (bytes_read == 0) {
537+
fprintf(stderr, "EOF reading %s\n", PROC_UPTIME_PATH);
538+
} else {
539+
if (bytes_read > buffer_size) {
540+
bytes_read = buffer_size;
541+
}
542+
buf[bytes_read] = '\0';
543+
530544
fprintf(stdout, "uptime %s\n", buf);
545+
}
531546

532547
close(fd);
533548
}

src/net.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,7 @@ static int hyper_get_ifindex(char *nic)
309309
{
310310
int fd, ifindex = -1;
311311
char path[512], buf[8];
312+
ssize_t bytes_read;
312313

313314
fprintf(stdout, "net device %s\n", nic);
314315
sprintf(path, "/sys/class/net/%s/ifindex", nic);
@@ -321,7 +322,8 @@ static int hyper_get_ifindex(char *nic)
321322
}
322323

323324
memset(buf, 0, sizeof(buf));
324-
if (read(fd, buf, sizeof(buf) - 1) <= 0) {
325+
bytes_read = read(fd, buf, sizeof(buf) - 1);
326+
if (bytes_read <= 0) {
325327
perror("can read open file");
326328
goto out;
327329
}

src/util.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -610,6 +610,7 @@ int hyper_open_channel(char *channel, int mode)
610610
struct dirent *dir;
611611
int fd = -1, i, num;
612612
char path[256], name[128];
613+
ssize_t bytes_read;
613614

614615
num = scandir("/sys/class/virtio-ports/", &list, NULL, NULL);
615616
if (num < 0) {
@@ -635,7 +636,8 @@ int hyper_open_channel(char *channel, int mode)
635636

636637
memset(name, 0, sizeof(name));
637638

638-
if (read(fd, name, sizeof(name)) < 0) {
639+
bytes_read = read(fd, name, sizeof(name));
640+
if (bytes_read < 0) {
639641
close(fd);
640642
continue;
641643
}

0 commit comments

Comments
 (0)