Blame SOURCES/nuxwdog-set-uid.patch

41cebb
From 3d7adfbe0788f33a67c3ed65e12ba9d32074a674 Mon Sep 17 00:00:00 2001
41cebb
From: Ade Lee <alee@redhat.com>
41cebb
Date: Mon, 15 Jan 2018 15:25:36 -0500
41cebb
Subject: [PATCH] Add parameter to set the uid of the invoked process
41cebb
41cebb
---
41cebb
 src/com/redhat/nuxwdog/watchdog.cpp | 36 ++++++++++++++++++++++++++++++++++--
41cebb
 src/com/redhat/nuxwdog/wdconf.cpp   |  7 +++++++
41cebb
 src/com/redhat/nuxwdog/wdconf.h     |  3 ++-
41cebb
 3 files changed, 43 insertions(+), 3 deletions(-)
41cebb
41cebb
diff --git a/src/com/redhat/nuxwdog/watchdog.cpp b/src/com/redhat/nuxwdog/watchdog.cpp
41cebb
index a4d6a77..36b13e4 100644
41cebb
--- a/src/com/redhat/nuxwdog/watchdog.cpp
41cebb
+++ b/src/com/redhat/nuxwdog/watchdog.cpp
41cebb
@@ -33,6 +33,7 @@
41cebb
 #include <stdlib.h>
41cebb
 #include <unistd.h>
41cebb
 #include <errno.h>
41cebb
+#include <cerrno>
41cebb
 #include <signal.h>
41cebb
 #include <fcntl.h>
41cebb
 #include <pwd.h>
41cebb
@@ -280,7 +281,7 @@ watchdog_exit(int status)
41cebb
 
41cebb
 int
41cebb
 _watchdog_exec(int server_starts, char *server_exe, char *args[], 
41cebb
-               char * envp[], int *spid)
41cebb
+               char * envp[], int *spid, int uid)
41cebb
 {
41cebb
     int server_background = 0;
41cebb
     char *server_out = NULL;
41cebb
@@ -412,6 +413,14 @@ _watchdog_exec(int server_starts, char *server_exe, char *args[],
41cebb
             free(server_context);
41cebb
         }
41cebb
 
41cebb
+        if (uid >= 0) {
41cebb
+            rv = setuid(uid);
41cebb
+            if (rv != 0) {
41cebb
+                watchdog_error("unable to setuid");
41cebb
+                watchdog_exit(1);
41cebb
+            }
41cebb
+        }
41cebb
+
41cebb
         rv = execv(server_exe, args);
41cebb
         if (rv < 0) {
41cebb
 	    watchdog_error("could not execute server binary");
41cebb
@@ -757,10 +766,12 @@ int main(int argc, char **argv, char **envp)
41cebb
     int ver=0;
41cebb
     int server_starts;
41cebb
     int server_stat;
41cebb
+    int uid=-1;
41cebb
     char *server_exe = NULL;
41cebb
     char *server_args = NULL;
41cebb
     char *conffile = NULL;
41cebb
     char *pch;
41cebb
+    char *user = NULL;
41cebb
     char *args[100];
41cebb
     struct stat statbuf;
41cebb
     UDS_NAME[0]=0;
41cebb
@@ -833,6 +844,11 @@ int main(int argc, char **argv, char **envp)
41cebb
         watchdog_exit(1);
41cebb
     }
41cebb
 
41cebb
+    /* user */
41cebb
+    if (confinfo->user) {
41cebb
+       user = strdup(confinfo->user);
41cebb
+    }
41cebb
+
41cebb
     if (detach) {
41cebb
         parent_watchdog_create_signal_handlers();
41cebb
 
41cebb
@@ -883,6 +899,22 @@ int main(int argc, char **argv, char **envp)
41cebb
         watchdog_exit(1);
41cebb
     }
41cebb
 
41cebb
+    if (user != NULL) {
41cebb
+        struct passwd *pw = getpwnam(user);
41cebb
+        if (pw == NULL) {
41cebb
+            sprintf(errmsgstr, "user %s does not exist", user);
41cebb
+            watchdog_error(errmsgstr);
41cebb
+            watchdog_exit(1);
41cebb
+        }
41cebb
+
41cebb
+        if (chown(UDS_NAME, pw->pw_uid, pw->pw_gid) != 0) {
41cebb
+            sprintf(errmsgstr, "chown failed errno %d %s", errno, strerror(errno));
41cebb
+            watchdog_error(errmsgstr);
41cebb
+            watchdog_exit(1);
41cebb
+        }
41cebb
+        uid = pw->pw_uid;
41cebb
+    }
41cebb
+
41cebb
     for (server_starts = 0;; ++server_starts) {
41cebb
 
41cebb
         _watchdog_death					= 0;
41cebb
@@ -895,7 +927,7 @@ int main(int argc, char **argv, char **envp)
41cebb
 
41cebb
         watchdog_create_signal_handlers();
41cebb
 
41cebb
-        rv = _watchdog_exec(server_starts, server_exe, args, envp, &server_pid);
41cebb
+        rv = _watchdog_exec(server_starts, server_exe, args, envp, &server_pid, uid);
41cebb
 
41cebb
         if (server_pid < 0) {
41cebb
             // exec failed:  kill parent if it's still waiting
41cebb
diff --git a/src/com/redhat/nuxwdog/wdconf.cpp b/src/com/redhat/nuxwdog/wdconf.cpp
41cebb
index 95603c9..2d50575 100644
41cebb
--- a/src/com/redhat/nuxwdog/wdconf.cpp
41cebb
+++ b/src/com/redhat/nuxwdog/wdconf.cpp
41cebb
@@ -158,6 +158,9 @@ _watchdog_parse_conffile(char *conffile,
41cebb
         if (!strcasecmp(name, "ChildSecurity")) {
41cebb
             info->childSecurity = atoi(value);
41cebb
         }
41cebb
+        if (!strcasecmp(name, "User")) {
41cebb
+            info->user = strdup(value);
41cebb
+        }
41cebb
         if (line != NULL) {
41cebb
             free(line);
41cebb
             line = NULL;
41cebb
@@ -227,5 +230,9 @@ watchdog_confinfo_free(watchdog_conf_info_t *info)
41cebb
         free(info->childPidFile);
41cebb
     }
41cebb
 
41cebb
+    if (info->user) {
41cebb
+        free(info->user);
41cebb
+    }
41cebb
+
41cebb
     free(info);
41cebb
 }
41cebb
diff --git a/src/com/redhat/nuxwdog/wdconf.h b/src/com/redhat/nuxwdog/wdconf.h
41cebb
index bb2e7b1..94f02e3 100644
41cebb
--- a/src/com/redhat/nuxwdog/wdconf.h
41cebb
+++ b/src/com/redhat/nuxwdog/wdconf.h
41cebb
@@ -36,7 +36,8 @@ typedef struct watchdog_conf_info_t {
41cebb
     char            *exeContext;       /* selinux type context */
41cebb
     char            *pidFile;          /* pidFile */
41cebb
     char            *childPidFile;     /* child pid file */
41cebb
-    int             childSecurity;     /* enforce child security */    
41cebb
+    int             childSecurity;     /* enforce child security */
41cebb
+    char            *user;             /* user to execute the process as */
41cebb
 } watchdog_conf_info_t;
41cebb
 
41cebb
 watchdog_conf_info_t *watchdog_parse(char *conf_file);
41cebb
-- 
41cebb
2.14.3
41cebb