From 15e11e7230fdb37da824b8150ebe33025073df03 Mon Sep 17 00:00:00 2001 From: "Jason A. Donenfeld" Date: Tue, 15 Jun 2021 12:39:39 +0200 Subject: script: use memfd_create instead of O_TMPFILE Signed-off-by: Jason A. Donenfeld --- Makefile | 2 +- cscript.c | 20 ++++++-------------- 2 files changed, 7 insertions(+), 15 deletions(-) diff --git a/Makefile b/Makefile index 5e2eb7b..74383b6 100644 --- a/Makefile +++ b/Makefile @@ -4,7 +4,7 @@ BINDIR ?= $(PREFIX)/bin MANDIR ?= $(PREFIX)/share/man CFLAGS ?= -O3 -march=native -CFLAGS += -std=c99 -Wall +CFLAGS += -std=gnu99 -Wall -D_GNU_SOURCE all: cscript diff --git a/cscript.c b/cscript.c index c7dbcf5..65ab92a 100644 --- a/cscript.c +++ b/cscript.c @@ -1,10 +1,8 @@ /* SPDX-License-Identifier: GPL-2.0 * - * Copyright (C) 2018 Jason A. Donenfeld . All Rights Reserved. + * Copyright (C) 2018-2021 Jason A. Donenfeld . All Rights Reserved. */ -#define _GNU_SOURCE - #include #include #include @@ -12,11 +10,12 @@ #include #include #include +#include #include int main(int argc, char *argv[], char *envp[]) { - int fd, fd2, input = -1, pipes[2] = { 0 }, status; + int fd, input = -1, pipes[2] = { 0 }, status; pid_t compiler_pid; char *output_path; @@ -34,9 +33,9 @@ int main(int argc, char *argv[], char *envp[]) } } - fd = open(getenv("HOME") ?: getenv("TMPDIR") ?: "/tmp", O_TMPFILE | O_EXCL | O_RDWR, S_IWUSR | S_IRUSR); + fd = memfd_create("cscript", 0); if (fd < 0) { - perror("Error: unable to create temporary inode"); + perror("Error: unable to create memfd"); return 1; } if (asprintf(&output_path, "/proc/self/fd/%d", fd) < 0) { @@ -94,14 +93,7 @@ int main(int argc, char *argv[], char *envp[]) return 1; } - fd2 = open(output_path, O_RDONLY); - if (fd2 < 0) { - perror("Error: unable to reopen temporary inode"); - return 1; - } - close(fd); - - if (fexecve(fd2, argv, envp) < 0) { + if (fexecve(fd, argv, envp) < 0) { perror("Error: could not execute compiled program"); return 1; } -- cgit v1.2.3-59-g8ed1b