libnftables: refuse to open onput files other than named pipes or regular files
Florian Westphal
maintainerPATCH 1/2 libnftables: refuse to open onput files other than named pipes or regular files
Don't start e.g. parsing a block device.
nftables is typically run as privileged user, exit early if we get unexpected input.
Only exception: Allow character device if input is /dev/stdin.
Signed-off-by Florian Westphal <[email protected]>
--- src/libnftables.c | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+)
src/libnftables.c +34 -0
@@ -17,6 +17,7 @@#include <cmd.h> #include <errno.h> #include <string.h> +#include <sys/stat.h> static int nft_netlink (struct nft_ctx *nft,struct list_head *cmds,struct list_head *msgs)
@@ -673,13 +674,46 @@ retry:return rc; } +/* need to use stat() to, fopen() will block for named fifos and +* libjansson makes no checks before or after open either. +*/ +static struct error_record *filename_is_useable (struct nft_ctx *nft,const char *name) +{ +unsigned int type; +struct stat sb; +int err; + + err =stat (name, &sb); +if (err) +return error (&internal_location,"Could not open file \"%s\": %s\n" , + name,strerror (errno)); + + type = sb.st_mode &S_IFMT ; + +if (type ==S_IFREG || type ==S_IFIFO ) +return NULL ; + +if (type ==S_IFCHR &&0 ==strcmp (name,"/dev/stdin" )) +return NULL ; + +return error (&internal_location,"Not a regular file: \"%s\"\n" , name); +} +static int __nft_run_cmd_from_filename (struct nft_ctx *nft,const char *filename) { +struct error_record *erec;struct cmd *cmd, *next;int rc, parser_rc;LIST_HEAD (msgs);LIST_HEAD (cmds); + erec =filename_is_useable (nft, filename); +if (erec) { +erec_print (&nft->output, erec, nft->debug_mask); +erec_destroy (erec); +return -1 ; + } + rc =load_cmdline_vars (nft, &msgs);if (rc <0 ) goto err;
Signature · 1 line
2.41.0
Florian Westphal
maintainerPATCH 2/2 scanner: restrict include directive to regular files
Similar to previous change, also check all
include "foo"
and reject those if they refer to named fifos, block devices etc.
Directories are still skipped, I don't think we can change this anymore.
Signed-off-by Florian Westphal <[email protected]>
--- src/scanner.l | 69 ++++++++++++++++++- .../testcases/bogons/nft-f/include-device | 1 + 2 files changed, 67 insertions(+), 3 deletions(-) create mode 100644 tests/shell/testcases/bogons/nft-f/include-device
src/scanner.l +66 -3
@@ -18,6 +18,7 @@ #include <arpa/inet.h> #include <linux/types.h> #include <linux/netfilter.h> +#include <sys/stat.h> #include <nftables.h> #include <erec.h>
@@ -972,9 +973,59 @@ static void scanner_push_file(struct nft_ctx *nft, void *scanner, scanner_push_indesc(state, indesc); } +enum nft_include_type { + NFT_INCLUDE, + NFT_CMDLINE, +}; + +static bool __is_useable(unsigned int type, enum nft_include_type t) +{ + type &= S_IFMT; + switch (type) { + case S_IFREG: return true; + case S_IFIFO: + return t == NFT_CMDLINE; /* disallow include /path/to/fifo */ + default: + break; + } + + return false; +} + +/* need to use stat() to, fopen() will block for named fifos */ +static bool filename_is_useable(const char *name) +{ + struct stat sb; + int err; + + err = stat(name, &sb); + if (err) + return false; + + return __is_useable(sb.st_mode, NFT_INCLUDE); +} + +static bool fp_is_useable(FILE *fp, enum nft_include_type t) +{ + int fd = fileno(fp); + struct stat sb; + int err; + + if (fd < 0) + return false; + + err = fstat(fd, &sb); + if (err < 0) + return false; + + return __is_useable(sb.st_mode, t); +} + static int include_file(struct nft_ctx *nft, void *scanner, const char *filename, const struct location *loc, - const struct input_descriptor *parent_indesc) + const struct input_descriptor *parent_indesc, + enum nft_include_type includetype) + { struct parser_state *state = yyget_extra(scanner); struct error_record *erec;
@@ -986,12 +1037,24 @@ static int include_file(struct nft_ctx *nft, void *scanner, goto err; } + if (includetype == NFT_INCLUDE && !filename_is_useable(filename)) { + erec = error(loc, "Not a regular file: \"%s\"\n", filename); + goto err; + } + f = fopen(filename, "r"); if (f == NULL) { erec = error(loc, "Could not open file \"%s\": %s\n", filename, strerror(errno)); goto err; } + + if (!fp_is_useable(f, includetype)) { + fclose(f); + erec = error(loc, "Not a regular file: \"%s\"\n", filename); + goto err; + } + scanner_push_file(nft, scanner, f, filename, loc, parent_indesc); return 0; err:
@@ -1064,7 +1127,7 @@ static int include_glob(struct nft_ctx *nft, void *scanner, const char *pattern, if (len == 0 || path[len - 1] == '/') continue; - ret = include_file(nft, scanner, path, loc, indesc); + ret = include_file(nft, scanner, path, loc, indesc, NFT_INCLUDE); if (ret != 0) goto err; }
@@ -1101,7 +1164,7 @@ err: int scanner_read_file(struct nft_ctx *nft, const char *filename, const struct location *loc) { - return include_file(nft, nft->scanner, filename, loc, NULL); + return include_file(nft, nft->scanner, filename, loc, NULL, NFT_CMDLINE); } static bool search_in_include_path(const char *filename)
tests/shell/testcases/bogons/nft-f/include-device new file +1 -0
@@ -0,0 +1 @@ +include "/dev/null"
Signature · 1 line
2.41.0