]> git.ipfire.org Git - pakfire.git/commitdiff
jail: Close the original fds for stdin/stdout after copying
authorMichael Tremer <michael.tremer@ipfire.org>
Thu, 4 Aug 2022 15:41:04 +0000 (15:41 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Thu, 4 Aug 2022 15:41:04 +0000 (15:41 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/jail.c

index 2d1778d31427f79aa12412c5b87acf7ac4df0ec6..078bb7f56db993a44479a6d6f90be0821c4844de 100644 (file)
@@ -482,6 +482,22 @@ static int pakfire_jail_handle_log(struct pakfire_jail* jail,
        return 0;
 }
 
+static int pakfire_jail_setup_pipe(struct pakfire_jail* jail, int (*fds)[2], const int flags) {
+       int r = pipe2(*fds, flags);
+       if (r < 0) {
+               ERROR(jail->pakfire, "Could not setup pipe: %m\n");
+               return 1;
+       }
+
+       return 0;
+}
+
+static void pakfire_jail_close_pipe(struct pakfire_jail* jail, int fds[2]) {
+       for (unsigned int i = 0; i < 2; i++)
+               if (fds[i])
+                       close(fds[i]);
+}
+
 /*
        This is a convenience function to fetch the reading end of a pipe and
        closes the write end.
@@ -1157,9 +1173,9 @@ static int pakfire_jail_child(struct pakfire_jail* jail, struct pakfire_jail_exe
                        return 1;
                }
 
-               // Close the reading sides of the pipe
-               close(ctx->pipes.stdout[0]);
-               close(ctx->pipes.stderr[0]);
+               // Close the pipe (as we have moved the original file descriptors)
+               pakfire_jail_close_pipe(jail, ctx->pipes.stdout);
+               pakfire_jail_close_pipe(jail, ctx->pipes.stderr);
        }
 
        // Reset open file limit (http://0pointer.net/blog/file-descriptor-limits.html)
@@ -1196,22 +1212,6 @@ static int pakfire_jail_child(struct pakfire_jail* jail, struct pakfire_jail_exe
        return r;
 }
 
-static int pakfire_jail_setup_pipe(struct pakfire_jail* jail, int (*fds)[2], const int flags) {
-       int r = pipe2(*fds, flags);
-       if (r < 0) {
-               ERROR(jail->pakfire, "Could not setup pipe: %m\n");
-               return 1;
-       }
-
-       return 0;
-}
-
-static void pakfire_jail_close_pipe(struct pakfire_jail* jail, int fds[2]) {
-       for (unsigned int i = 0; i < 2; i++)
-               if (fds[i])
-                       close(fds[i]);
-}
-
 // Run a command in the jail
 static int __pakfire_jail_exec(struct pakfire_jail* jail, const char* argv[]) {
        int exit = -1;