eBPF-based security platform
io: read settings files a whole line at a time
Daniel Borkmanna27b17481edf
diff --git a/builtin-bpm.c b/builtin-bpm.cindex 12b91e68..69d725e7 100644--- a/builtin-bpm.c+++ b/builtin-bpm.c@@ -341,9 +341,10 @@ static FILE *tree_conf_open(void)static int load_source(const char *name, struct bpm_source *s){- char path[PATH_MAX], line[PATH_MAX];+ char path[PATH_MAX], *line = NULL;+ size_t capacity = 0;+ int got, rc = 0;FILE *fp;- int rc;memset(s, 0, sizeof(*s));rc = source_path(name, path, sizeof(path));@@ -356,17 +357,19 @@ static int load_source(const char *name, struct bpm_source *s)fclose(fp);return -EPERM;}- strscpy(s->name, name, sizeof(s->name));- while (fgets(line, sizeof(line), fp)) {- rc = bpm_parse_source_line(line,+ if (strscpy(s->name, name, sizeof(s->name)) < 0)+ rc = -ENAMETOOLONG;+ while (!rc && (got = read_text_line(fp, &line, &capacity,+ PATH_MAX - 1)) != 0) {+ rc = got < 0 ? got :+ bpm_parse_source_line(line,s->endpoint, sizeof(s->endpoint),s->key_file, sizeof(s->key_file));- if (rc) {- fclose(fp);- return rc;- }}+ libc_free(line);fclose(fp);+ if (rc)+ return rc;if (!s->endpoint[0] || !s->key_file[0])return -EINVAL;/* With a config image the key file is the image's, named from its root. */@@ -1568,7 +1571,9 @@ struct bpm_tree_config {static int load_tree_config(struct bpm_tree_config *cfg){- char line[PATH_MAX];+ size_t capacity = 0;+ char *line = NULL;+ int got, rc = 0;FILE *fp;*cfg = (struct bpm_tree_config){};@@ -1579,16 +1584,19 @@ static int load_tree_config(struct bpm_tree_config *cfg)fclose(fp);return -EPERM;}- while (fgets(line, sizeof(line), fp)) {- if (bpm_parse_tree_line(line, cfg->endpoint,+ while (!rc && (got = read_text_line(fp, &line, &capacity,+ PATH_MAX - 1)) != 0) {+ if (got < 0 ||+ bpm_parse_tree_line(line, cfg->endpoint,sizeof(cfg->endpoint),cfg->reference,- sizeof(cfg->reference))) {- fclose(fp);- return -EINVAL;- }+ sizeof(cfg->reference)))+ rc = -EINVAL;}+ libc_free(line);fclose(fp);+ if (rc)+ return rc;if (!cfg->endpoint[0] || !cfg->reference[0])return -EINVAL;return 0;diff --git a/util/bpm_tree_state.c b/util/bpm_tree_state.cindex b2f3295b..0318a937 100644--- a/util/bpm_tree_state.c+++ b/util/bpm_tree_state.c@@ -143,13 +143,19 @@ static int read_config_file(const char *path, struct shirudo_tree_kv *out,static int read_config_stream(FILE *f, struct shirudo_tree_kv *out, int *n,int max){- char line[512];- int rc = 0;+ size_t capacity = 0;+ char *line = NULL;+ int got, rc = 0;*n = 0;- while (fgets(line, sizeof(line), f)) {+ while ((got = read_text_line(f, &line, &capacity, 511)) != 0) {char *start = line, *end, *eq;+ if (got < 0) {+ rc = got;+ break;+ }+while (*start && isspace((__u8)*start))start++;if (*start == '#' || *start == '\0')@@ -170,6 +176,7 @@ static int read_config_stream(FILE *f, struct shirudo_tree_kv *out, int *n,if (rc)break;}+ libc_free(line);fclose(f);return rc;}diff --git a/util/config.c b/util/config.cindex 23a8c424..5b7dedf1 100644--- a/util/config.c+++ b/util/config.c@@ -21,6 +21,7 @@#include "config.h"#include "hex.h"#include "io.h"+#include "malloc.h"#include "json.h"#include "image_config.h"#include "log.h"@@ -206,25 +207,29 @@ static void set_owner_auth(struct shirudo_config *cfg, const char *val)static int config_parse(struct shirudo_config *cfg, FILE *f,const char *path){- char line[SHIRUDO_MAP_EXPORT_PATH_MAX + 128];- int rc = 0;+ const size_t max = SHIRUDO_MAP_EXPORT_PATH_MAX + 127;+ size_t capacity = 0;+ char *line = NULL;+ int got, rc = 0;if (check_trusted(fileno(f), path, false)) {fclose(f);return -EPERM;}- while (fgets(line, sizeof(line), f)) {+ while ((got = read_text_line(f, &line, &capacity, max)) != 0) {char *p, *eq, *key, *val;- int c;-- /* Never take a line the buffer cut short. */- if (!strchr(line, '\n') && !feof(f)) {- pr_warn("config: line longer than %zu bytes "- "(ignored)", sizeof(line) - 1);- while ((c = fgetc(f)) != EOF && c != '\n')- continue;++ if (got == -E2BIG) {+ pr_warn("config: line longer than %zu bytes (ignored)",+ max);continue;}+ if (got < 0) {+ pr_err("config: %s: unreadable line: %s", path,+ strerror(-got));+ rc = got;+ break;+ }p = trim(line);if (*p == '\0' || *p == '#')continue;@@ -333,6 +338,11 @@ static int config_parse(struct shirudo_config *cfg, FILE *f,cfg->tpm_owner_auth_len = 0;rc = -EPERM;}+ if (line) {+ /* The owner's authorization may have been read into it. */+ explicit_bzero(line, capacity);+ libc_free(line);+ }if (fclose(f) && !rc)rc = -errno;return rc;diff --git a/util/image_bootstrap.c b/util/image_bootstrap.cindex bb63f19c..e2649d26 100644--- a/util/image_bootstrap.c+++ b/util/image_bootstrap.c@@ -14,6 +14,8 @@#include "config_measure.h"#include "hex.h"#include "image_bootstrap.h"+#include "io.h"+#include "malloc.h"#include "paths.h"bool shirudo_image_path_valid(const char *path)@@ -41,14 +43,16 @@ static char *trim(char *text)static int bootstrap_parse(FILE *file, struct shirudo_image_bootstrap *boot,bool initial){- char line[PATH_MAX + 128];- int rc = 0;+ size_t capacity = 0;+ char *line = NULL;+ int got, rc = 0;- while (fgets(line, sizeof(line), file)) {+ while ((got = read_text_line(file, &line, &capacity,+ PATH_MAX + 127)) != 0) {char *key, *value;- if (!strchr(line, '\n') && !feof(file)) {- rc = -E2BIG;+ if (got < 0) {+ rc = got;break;}key = trim(line);@@ -109,6 +113,7 @@ static int bootstrap_parse(FILE *file, struct shirudo_image_bootstrap *boot,}if (ferror(file) && !rc)rc = -EIO;+ libc_free(line);fclose(file);return rc;}diff --git a/util/io.c b/util/io.cindex 8ba3c7dc..febdc3eb 100644--- a/util/io.c+++ b/util/io.c@@ -5,6 +5,7 @@#include <fcntl.h>#include <limits.h>#include <stdint.h>+#include <stdio.h>#include <stdlib.h>#include <string.h>#include <sys/stat.h>@@ -103,6 +104,28 @@ int read_fd(int fd, const char *label, size_t max, void *out, size_t *len)return 0;}+/*+ * One line of text, at most max bytes before its newline, in getline's+ * buffer, which the caller frees with libc_free: 1, 0 at the end, -E2BIG+ * for a longer line and -EINVAL for one holding a NUL. fgets cut a long+ * line into two that each parsed, and a NUL ended the string where the+ * line went on.+ */+int read_text_line(FILE *f, char **line, size_t *cap, size_t max)+{+ ssize_t n;++ errno = 0;+ n = getline(line, cap, f);+ if (n < 0)+ return errno ? -errno : 0;+ if (memchr(*line, '\0', (size_t)n))+ return -EINVAL;+ if ((*line)[n - 1] == '\n')+ n--;+ return (size_t)n > max ? -E2BIG : 1;+}+int read_file(const char *path, size_t max, void *out, size_t *len){int fd, rc;diff --git a/util/io.h b/util/io.hindex 16ccc56e..24002f65 100644--- a/util/io.h+++ b/util/io.h@@ -6,6 +6,7 @@#include <stdbool.h>#include <stddef.h>+#include <stdio.h>int write_complete(int fd, const void *buf, size_t len);@@ -19,6 +20,7 @@ int read_fd(int fd, const char *label, size_t max, void *out, size_t *len);int read_file_at(int dirfd, const char *rel, size_t max, void *out,size_t *len);int open_beneath(int dirfd, const char *rel, int flags);+int read_text_line(FILE *f, char **line, size_t *cap, size_t max);/** Whether the file open on fd at path may steer what shirudo trusts.