From f50e179100cf6a7e9880f7250b4dc91231b70432 Mon Sep 17 00:00:00 2001 From: Riccardo Magliocchetti Date: Sat, 26 Jan 2013 21:00:11 +0100 Subject: [PATCH 1/4] core: don't call dup2 on negative fd after open() error Also call close() only if open was succesful. Reported by Coverity as CID #970991. --- core/logging.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/core/logging.c b/core/logging.c index 56ee5ff5..bb45aa37 100644 --- a/core/logging.c +++ b/core/logging.c @@ -458,13 +458,13 @@ void uwsgi_check_logrotate(void) { uwsgi_error_open(uwsgi.logfile); grace_them_all(0); } - if (dup2(fd, uwsgi.original_log_fd) < 0) { - uwsgi_error("dup2()"); - grace_them_all(0); + else { + if (dup2(fd, uwsgi.original_log_fd) < 0) { + uwsgi_error("dup2()"); + grace_them_all(0); + } + close(fd); } - - close(fd); - } else { uwsgi_error("unable to rotate log: rename()"); From 838bdd27a6db0afb7ff09b98ab26c9a7595066de Mon Sep 17 00:00:00 2001 From: Riccardo Magliocchetti Date: Sat, 26 Jan 2013 21:10:33 +0100 Subject: [PATCH 2/4] core: check return of getgroups() When we pass 0 as size to getgroups() the number of additional groups is return. If it returns error or 0 don't even try to poke for additional groups. Reported by Coverity as CID #970944. --- core/utils.c | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/core/utils.c b/core/utils.c index 248efab5..79898343 100644 --- a/core/utils.c +++ b/core/utils.c @@ -460,18 +460,20 @@ void uwsgi_as_root() { } } int additional_groups = getgroups(0, NULL); - gid_t *gids = uwsgi_calloc(sizeof(gid_t) * additional_groups); - int i; - if (getgroups(additional_groups, gids) > 0) { - for (i = 0; i < additional_groups; i++) { - if (gids[i] == uwsgi.gid) - continue; - struct group *gr = getgrgid(gids[i]); - if (gr) { - uwsgi_log("set additional group %d (%s)\n", gids[i], gr->gr_name); - } - else { - uwsgi_log("set additional group %d\n", gids[i]); + if (additional_groups > 0) { + gid_t *gids = uwsgi_calloc(sizeof(gid_t) * additional_groups); + if (getgroups(additional_groups, gids) > 0) { + int i; + for (i = 0; i < additional_groups; i++) { + if (gids[i] == uwsgi.gid) + continue; + struct group *gr = getgrgid(gids[i]); + if (gr) { + uwsgi_log("set additional group %d (%s)\n", gids[i], gr->gr_name); + } + else { + uwsgi_log("set additional group %d\n", gids[i]); + } } } } From 297bd429a9bc34c70832d60bedbf6db4e42616ea Mon Sep 17 00:00:00 2001 From: Riccardo Magliocchetti Date: Sat, 26 Jan 2013 21:18:55 +0100 Subject: [PATCH 3/4] core: check return of uwsgi_connect in uwsgi_opt_connect_and_read Reported by Coverity as CID #970995. --- core/uwsgi.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/core/uwsgi.c b/core/uwsgi.c index dee67112..e3aa00eb 100644 --- a/core/uwsgi.c +++ b/core/uwsgi.c @@ -4035,7 +4035,7 @@ void uwsgi_opt_connect_and_read(char *opt, char *address, void *foobar) { char buf[8192]; int fd = uwsgi_connect(address, -1, 0); - for (;;) { + while (fd >= 0) { int ret = uwsgi_waitfd(fd, -1); if (ret <= 0) { exit(0); @@ -4046,6 +4046,8 @@ void uwsgi_opt_connect_and_read(char *opt, char *address, void *foobar) { } uwsgi_log("%.*s", (int) len, buf); } + uwsgi_error("uwsgi_connect()"); + exit(1); } void uwsgi_opt_extract(char *opt, char *address, void *foobar) { From e106a6ef614cc857878d5af2f9dde0ac5b1edc35 Mon Sep 17 00:00:00 2001 From: Riccardo Magliocchetti Date: Sat, 26 Jan 2013 21:23:17 +0100 Subject: [PATCH 4/4] plugins/python: check uwsgi_connect return code in uwsgi_python_harakiri Reported by Coverity as CID #970996. --- plugins/python/python_plugin.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/python/python_plugin.c b/plugins/python/python_plugin.c index 9ad87a28..b183bdef 100644 --- a/plugins/python/python_plugin.c +++ b/plugins/python/python_plugin.c @@ -1802,7 +1802,7 @@ void uwsgi_python_harakiri(int wid) { char *address = uwsgi_concat2(up.tracebacker, uwsgi_num2str(wid)); int fd = uwsgi_connect(address, -1, 0); - for (;;) { + while (fd >= 0) { int ret = uwsgi_waitfd(fd, uwsgi.shared->options[UWSGI_OPTION_SOCKET_TIMEOUT]); if (ret <= 0) { break;