walters / rpms / nfs-utils

Forked from rpms/nfs-utils 6 years ago
Clone
0023908
commit 33bbeabb40d11a59266e0702adaa6a2e0acb6382
0023908
Author: Neil Brown <neilb@suse.de>
0023908
Date:   Wed Nov 26 12:01:06 2008 -0500
0023908
0023908
    Ensure statd gets started if required when non-root
0023908
    user mounts an NFS filesystem.
0023908
    
0023908
    The first time an NFS filesystem is mounted, we start statd from
0023908
    /sbin/mount.nfs.  If this first time is a non-root user doing the
0023908
     mount, (thanks to e.g.  the 'users' option in /etc/fstab)
0023908
    then we need to be sure that the 'setuid' status from mount.nfs
0023908
    is inherited through to rpc.statd so that it runs as root.
0023908
    
0023908
    There are two places where we loose our setuid status due to the shell
0023908
    (/bin/sh) discarding.
0023908
    
0023908
    1/ mount.nfs uses "system" to run /usr/sbin/start-statd.  This runs a
0023908
       shell which is likely to drop privileges.  So change that code to use
0023908
      'fork' and 'execl' explicitly.
0023908
    2/ start-statd is a shell script.  To convince the shell to allow the
0023908
      program to run in privileged mode, we need to add a "-p" flag.
0023908
    
0023908
    We could just call setuid(getuid()) at some appropriate time, and it
0023908
    might be worth doing that as well, however I think that getting
0023908
    rid of 'system()' is a good idea and once that is done, the
0023908
    adding of '-p' is trivial and sufficient.
0023908
    
0023908
    Signed-off-by: Neil Brown <neilb@suse.de>
0023908
    Signed-off-by: Steve Dickson <steved@redhat.com>
0023908
0023908
diff --git a/utils/mount/network.c b/utils/mount/network.c
0023908
index 2db694d..806344c 100644
0023908
--- a/utils/mount/network.c
0023908
+++ b/utils/mount/network.c
0023908
@@ -36,6 +36,7 @@
0023908
 
0023908
 #include <sys/types.h>
0023908
 #include <sys/socket.h>
0023908
+#include <sys/wait.h>
0023908
 #include <netinet/in.h>
0023908
 #include <rpc/rpc.h>
0023908
 #include <rpc/pmap_prot.h>
0023908
@@ -705,7 +706,18 @@ int start_statd(void)
0023908
 #ifdef START_STATD
0023908
 	if (stat(START_STATD, &stb) == 0) {
0023908
 		if (S_ISREG(stb.st_mode) && (stb.st_mode & S_IXUSR)) {
0023908
-			system(START_STATD);
0023908
+			pid_t pid = fork();
0023908
+			switch (pid) {
0023908
+			case 0: /* child */
0023908
+				execl(START_STATD, START_STATD, NULL);
0023908
+				exit(1);
0023908
+			case -1: /* error */
0023908
+				perror("Fork failed");
0023908
+				break;
0023908
+			default: /* parent */
0023908
+				waitpid(pid, NULL,0);
0023908
+				break;
0023908
+			}
0023908
 			if (probe_statd())
0023908
 				return 1;
0023908
 		}
0023908
diff --git a/utils/statd/start-statd b/utils/statd/start-statd
0023908
index 6e7ea04..c7805ee 100644
0023908
--- a/utils/statd/start-statd
0023908
+++ b/utils/statd/start-statd
0023908
@@ -1,4 +1,4 @@
0023908
-#!/bin/sh
0023908
+#!/bin/sh -p
0023908
 # nfsmount calls this script when mounting a filesystem with locking
0023908
 # enabled, but when statd does not seem to be running (based on
0023908
 # /var/run/rpc.statd.pid).