GNOME Bugzilla – Bug 781785
Coverity complains about the possibility of 'close (-1)'
Last modified: 2017-05-02 12:31:20 UTC
Coverity says: 4. gnome-keyring-3.20.0/daemon/gkd-main.c:916: negative_return_fn: Function "fork_and_print_environment()" returns a negative number. 6. gnome-keyring-3.20.0/daemon/gkd-main.c:637:3: return_negative_constant: Explicitly returning negative value "-1". 7. gnome-keyring-3.20.0/daemon/gkd-main.c:916: var_assign: Assigning: signed variable "parent_wakeup_fd" = "fork_and_print_environment". 18. gnome-keyring-3.20.0/daemon/gkd-main.c:978: negative_returns: "parent_wakeup_fd" is passed to a parameter that cannot be negative. # 976| /* Print the environment and tell the parent we're done */ # 977| print_environment (); # 978|-> close (parent_wakeup_fd); # 979| # 980| if (!run_foreground) This code originates from: commit 6040bdb2a0ee9ee1f8e308b8cdede6d3fe5c94bf Author: Ray Strode <rstrode@redhat.com> Date: Thu Oct 15 14:37:33 2015 -0400 daemon: fork before threads are spawned It's not really a good idea to fork after glib has initialized, since it has helper threads that may have taken locks etc. This commit forks really early to prevent locks from leaking and causing deadlock. Signed-off-by: Cosimo Cecchi <cosimoc@gnome.org> * Split out separate function * Check return value of g_unix_open_pipe() * Don't fork when running foreground * Read login password before fork() Signed-off-by: Stef Walter <stefw@gnome.org> * Since stdout is open, just print evironment directly https://bugzilla.gnome.org/show_bug.cgi?id=756059 I think the problem arises with the --foreground option. run_foreground == TRUE and fork_and_print_environment() returns -1. Hence parent_wakeup_fd == -1, and close might be called with a negative value. Even though close(-1) might not be a problem in practice, it will be nice to not rely on that to avoid exercising lesser-used code paths in libc and the kernel.
Created attachment 350499 [details] [review] This should help, but it's untested. -- main: don't call close() with a negative value When run_foreground = TRUE, fork_and_print_environment() will return -1, which is assigned to parent_wakeup_fd. Since we don't want to call close(-1), only close(parent_wakeup_fd) when run_foreground = FALSE.
Looks goood. But I'm going to run this for a few days before merging it. If I forget about it, please ping me.
Fixed.