Skip to contentSkip to conversation

parser_json: use stdin buffer if available

PATCH In review Pablo Neira Ayuso 2 participants 4 messages Jul 10, 2024 netfilter

lore
Share & tools
lore

Pablo Neira Ayuso

author maintainer
#0 lore raw

PATCH 1/2 parser_json: use stdin buffer if available

Since 5c2b2b0a2ba7 ("src: error reporting with -f and read from stdin") stdin is stored in a buffer, update json support to use it instead of reading from /dev/stdin.

Some systems do not provide /dev/stdin symlink to /proc/self/fd/0 according to reporter (that mentions Yocto Linux as example).

Fixes 935f82e7dd49 ("Support 'nft -f -' to read from stdin")

---
v2: remove check for nft_output_json() in nft_run_cmd_from_filename()
as suggested by Phil Sutter, so JSON support does not really use /dev/stdin.

 src/libnftables.c | 3 +--
 src/parser_json.c | 7 +++++++
 2 files changed, 8 insertions(+), 2 deletions(-)
src/libnftables.c +1 -2
index af4734c05004..89317f9f6049 100644
@@ -807,8 +807,7 @@ int nft_run_cmd_from_filename(struct nft_ctx *nft, const char *filename)
 	if (!strcmp(filename, "-"))
 		filename = "/dev/stdin";
 
-	if (!strcmp(filename, "/dev/stdin") &&
-	    !nft_output_json(&nft->output))
+	if (!strcmp(filename, "/dev/stdin"))
 		nft->stdin_buf = stdin_to_buffer();
 
 	if (!nft->stdin_buf &&
src/parser_json.c +7 -0
index ee4657ee8044..4912d3608b2b 100644
@@ -4357,6 +4357,13 @@ int nft_parse_json_filename(struct nft_ctx *nft, const char *filename,
 	json_error_t err;
 	int ret;
 
+	if (nft->stdin_buf) {
+		json_indesc.type = INDESC_STDIN;
+		json_indesc.name = "/dev/stdin";
+
+		return nft_parse_json_buffer(nft, nft->stdin_buf, msgs, cmds);
+	}
+
 	json_indesc.type = INDESC_FILE;
 	json_indesc.name = filename;
 
Signature · 1 line
2.30.2
author maintainer
#1 lore raw

PATCH 2/2 libnftables: skip useable checks for /dev/stdin

/dev/stdin is a placeholder, read() from STDIN_FILENO is used to fetch the standard input into a buffer.

Since 5c2b2b0a2ba7 ("src: error reporting with -f and read from stdin") stdin is stored in a buffer to fix error reporting.

This patch requires: ("parser_json: use stdin buffer if available")

Fixes 149b1c95d129 ("libnftables: refuse to open onput files other than named pipes or regular files")

---
v2: no changes.

 src/libnftables.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
src/libnftables.c +4 -3
index 89317f9f6049..36d6a854ff50 100644
@@ -664,6 +664,7 @@ retry:
 
 /* need to use stat() to, fopen() will block for named fifos and
  * libjansson makes no checks before or after open either.
+ * /dev/stdin is *never* used, read() from STDIN_FILENO is used instead.
  */
 static struct error_record *filename_is_useable(struct nft_ctx *nft, const char *name)
 {
@@ -671,6 +672,9 @@ static struct error_record *filename_is_useable(struct nft_ctx *nft, const char
 	struct stat sb;
 	int err;
 
+	if (!strcmp(name, "/dev/stdin"))
+		return NULL;
+
 	err = stat(name, &sb);
 	if (err)
 		return error(&internal_location, "Could not open file \"%s\": %s\n",
@@ -681,9 +685,6 @@ static struct error_record *filename_is_useable(struct nft_ctx *nft, const char
 	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);
 }
 
Signature · 1 line
2.30.2
reviewer Acked-by on v1 1/2
#2 lore raw
In reply to Pablo Neira Ayuso
Quoted 7 lines · Pablo Neira Ayuso

Since 5c2b2b0a2ba7 ("src: error reporting with -f and read from stdin") stdin is stored in a buffer, update json support to use it instead of reading from /dev/stdin.

Some systems do not provide /dev/stdin symlink to /proc/self/fd/0 according to reporter (that mentions Yocto Linux as example).

Quoted 6 lines
Fixes 935f82e7dd49 ("Support 'nft -f -' to read from stdin")

---
v2: remove check for nft_output_json() in nft_run_cmd_from_filename()
as suggested by Phil Sutter, so JSON support does not really use /dev/stdin.

Series:

Thanks, Phil

author maintainer
#3 lore raw
Quoted 15 lines · Phil Sutter
Quoted 7 lines · Pablo Neira Ayuso

Since 5c2b2b0a2ba7 ("src: error reporting with -f and read from stdin") stdin is stored in a buffer, update json support to use it instead of reading from /dev/stdin.

Some systems do not provide /dev/stdin symlink to /proc/self/fd/0 according to reporter (that mentions Yocto Linux as example).

Quoted 6 lines
Fixes 935f82e7dd49 ("Support 'nft -f -' to read from stdin")

---
v2: remove check for nft_output_json() in nft_run_cmd_from_filename()
as suggested by Phil Sutter, so JSON support does not really use /dev/stdin.

Quoted 2 lines

Series:

Pushed out, thanks