Red Hat axp-list: Linux on Alpha (1995-2022)
 help / color / mirror / Atom feed
* SUID scripts?
@ 1998-08-06 18:42 Christopher Curtis
  1998-08-06 18:55 ` Jeff Sturm
  0 siblings, 1 reply; 6+ messages in thread
From: Christopher Curtis @ 1998-08-06 18:42 UTC (permalink / raw)
  To: axp-list

I'm trying to write a SUID shellscript, but am having problems.

The script:

-----------------------------------------------------------------------
#!/bin/sh

echo $UID
echo $EUID
-----------------------------------------------------------------------

The perms:

-rwsrwx---   1 root     www          1521 Aug  6 14:40 cd-share

The filesystem:

/dev/sda5               /usr                    ext2    defaults       1 2

The output:

-----------------------------------------------------------------------
308
308
-----------------------------------------------------------------------

Is there something here that I'm totally missing?  Redhat 4.2

--
Christopher Curtis               - http://www.ee.fit.edu/users/ccurtis
Florida Institute of Technology  - System Administrator, Programmer
Melbourne, Florida  USA          - http://www.lp.org/


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: SUID scripts?
  1998-08-06 18:42 SUID scripts? Christopher Curtis
@ 1998-08-06 18:55 ` Jeff Sturm
  1998-08-07  0:23   ` Mike "Ford" Ditto
  0 siblings, 1 reply; 6+ messages in thread
From: Jeff Sturm @ 1998-08-06 18:55 UTC (permalink / raw)
  To: axp-list


Setuid shell scripts are considered dangerous.  I don't think they work 
on any Linux platform.  Alternatively, you can use perl or a C wrapper 
binary (but beware of environment variables, if security is an issue).

Jeff


On Thu, 6 Aug 1998, Christopher Curtis wrote:

> I'm trying to write a SUID shellscript, but am having problems.
> 
> The script:
> 
> -----------------------------------------------------------------------
> #!/bin/sh
> 
> echo $UID
> echo $EUID
> -----------------------------------------------------------------------
> 
> The perms:
> 
> -rwsrwx---   1 root     www          1521 Aug  6 14:40 cd-share
> 
> The filesystem:
> 
> /dev/sda5               /usr                    ext2    defaults       1 2
> 
> The output:
> 
> -----------------------------------------------------------------------
> 308
> 308
> -----------------------------------------------------------------------
> 
> Is there something here that I'm totally missing?  Redhat 4.2
> 
> --
> Christopher Curtis               - http://www.ee.fit.edu/users/ccurtis
> Florida Institute of Technology  - System Administrator, Programmer
> Melbourne, Florida  USA          - http://www.lp.org/
> 
> -- 
> To unsubscribe: send e-mail to axp-list-request@redhat.com with
> 'unsubscribe' as the subject.  Do not send it to axp-list@redhat.com
> 


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: SUID scripts?
  1998-08-06 18:55 ` Jeff Sturm
@ 1998-08-07  0:23   ` Mike "Ford" Ditto
  1998-08-07  4:03     ` Michal Jaegermann
  0 siblings, 1 reply; 6+ messages in thread
From: Mike "Ford" Ditto @ 1998-08-07  0:23 UTC (permalink / raw)
  To: axp-list; +Cc: linux-kernel, Jeff Sturm

> From: Christopher Curtis <ccurtis@ee.fit.edu>
>
> I'm trying to write a SUID shellscript, but am having problems.

> From: Jeff Sturm <jsturm@gatecom.com>
>
> Setuid shell scripts are considered dangerous.  I don't think they work
> on any Linux platform.  Alternatively, you can use perl or a C wrapper
> binary (but beware of environment variables, if security is an issue).


Here is an implementation of "secure" set-ID #! files (A.K.A. set-uid
shell scripts) for Linux 2.0.x.  I have been running it for over a year
but as you can see, it still has a printk or two which reveal that I
don't actually run very many set-ID shell scripts (the printk's would
get annoying).  You can remove the printk's or leave them in if you're
paranoid or want to know when someone is probing for a security hole in
your system.

This patch implements set-ID #! shell scripts and makes them as secure
as they are under Solaris, Unix SVR4, etc.  The major hole that would
otherwise make set-ID scripts insecure (replacement of the file
inbetween the exec and the shell calling open()) is eliminated by
changing the shell's argv[1] to the name of a file descriptor to the
script file opened by the kernel during exec().  There can, of course,
be other security holes due to the contents of your script.  Be careful.

Also included in this patch is a fix for a bug in /proc which
prevented a process from examining itself under some conditions.

I'm running 2.0.33 with this patch.  I haven't yet looked to see how much
would have to change for 2.1.x.
					-=] Ford [=-

"I can stand the sight of worms		(In Real Life:  Mike Ditto)
 or look at microscopic germs 		ford@omnicron.com
 but technicolor pachyderms		http://www.omnicron.com/~ford/ford.html
 are really too much for me."

--- include/linux/proc_fs.h.orig	Wed Apr  8 15:38:22 1998
+++ include/linux/proc_fs.h	Thu Aug  6 16:58:52 1998
@@ -256,6 +256,7 @@
 extern int init_proc_fs(void);
 extern struct inode * proc_get_inode(struct super_block *, int, struct proc_dir_entry *);
 extern void proc_statfs(struct super_block *, struct statfs *, int);
+extern int proc_permission(struct inode *, int);
 extern void proc_read_inode(struct inode *);
 extern void proc_write_inode(struct inode *);
 extern int proc_match(int, const char *, struct proc_dir_entry *);
--- fs/proc/link.c.orig	Sat Nov  1 12:31:42 1997
+++ fs/proc/link.c	Thu Aug  6 16:45:25 1998
@@ -58,7 +58,7 @@
 	NULL,			/* writepage */
 	NULL,			/* bmap */
 	NULL,			/* truncate */
-	NULL			/* permission */
+	proc_permission		/* permission */
 };
 
 
--- fs/proc/fd.c.orig	Wed Nov 12 20:36:41 1997
+++ fs/proc/fd.c	Thu Aug  6 16:45:25 1998
@@ -49,7 +49,7 @@
 	NULL,			/* writepage */
 	NULL,			/* bmap */
 	NULL,			/* truncate */
-	NULL			/* permission */
+	proc_permission		/* permission */
 };
 
 static int proc_lookupfd(struct inode * dir, const char * name, int len,
--- fs/proc/inode.c.orig	Sat Nov  1 12:31:17 1997
+++ fs/proc/inode.c	Thu Aug  6 16:52:26 1998
@@ -129,6 +129,44 @@
 	memcpy_tofs(buf, &tmp, bufsiz);
 }
 
+int proc_permission(struct inode * inode, int mask)
+{
+	unsigned long ino, pid;
+	struct task_struct * p;
+	int i, mode;
+
+	ino = inode->i_ino;
+	pid = ino >> 16;
+	p = task[0];
+	for (i = 0; i < NR_TASKS ; i++)
+		if ((p = task[i]) && (p->pid == pid))
+			break;
+	if (!p || i >= NR_TASKS)
+		return -ENOENT;
+
+	/* If the process is trying to examine itself, allow any access. */
+	if (current == p ||
+	    (current->euid == p->euid && current->uid == p->uid &&
+	     current->egid == p->egid && current->gid == p->gid))
+		return 0;
+
+	/* Otherwise just duplicate what permission() in namei.c would do. */
+	mode = inode->i_mode;
+	if ((mask & S_IWOTH) && IS_RDONLY(inode) &&
+		 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
+		return -EROFS; /* Nobody gets write access to a read-only fs */
+	else if ((mask & S_IWOTH) && IS_IMMUTABLE(inode))
+		return -EACCES; /* Nobody gets write access to an immutable file */
+	else if (current->fsuid == inode->i_uid)
+		mode >>= 6;
+	else if (in_group_p(inode->i_gid))
+		mode >>= 3;
+	if (((mode & mask & 0007) == mask) || fsuser())
+		return 0;
+	return -EACCES;
+}
+
+
 void proc_read_inode(struct inode * inode)
 {
 	unsigned long ino, pid;
--- fs/exec.c.orig	Wed Dec 10 18:09:44 1997
+++ fs/exec.c	Thu Aug  6 13:33:30 1998
@@ -522,8 +522,6 @@
 	if (bprm->inode->i_writecount > 0)
 		return -ETXTBSY;
 
-	bprm->e_uid = current->euid;
-	bprm->e_gid = current->egid;
 	id_change = 0;
 
 	/* Set-uid? */
@@ -676,6 +674,8 @@
 	if ((bprm.envc = count(envp)) < 0)
 		return bprm.envc;
 
+	bprm.e_uid = current->euid;
+	bprm.e_gid = current->egid;
 	retval = prepare_binprm(&bprm);
 	
 	if(retval>=0) {
--- fs/binfmt_script.c.orig	Sat Mar 23 04:46:40 1996
+++ fs/binfmt_script.c	Thu Aug  6 16:21:08 1998
@@ -14,7 +14,8 @@
 static int do_load_script(struct linux_binprm *bprm,struct pt_regs *regs)
 {
 	char *cp, *interp, *i_name, *i_arg;
-	int retval;
+	int retval, script_fd;
+	char buf[20];
 	if ((bprm->buf[0] != '#') || (bprm->buf[1] != '!') || (bprm->sh_bang)) 
 		return -ENOEXEC;
 	/*
@@ -23,7 +24,9 @@
 	 */
 
 	bprm->sh_bang++;
-	iput(bprm->inode);
+	/* dont_iput prevents other binfmt types from examining buf after we
+	   modify it here, but we have to make sure to do iput in any early
+	   error return cases. */
 	bprm->dont_iput=1;
 
 	bprm->buf[127] = '\0';
@@ -38,8 +41,10 @@
 			break;
 	}
 	for (cp = bprm->buf+2; (*cp == ' ') || (*cp == '\t'); cp++);
-	if (!cp || *cp == '\0') 
+	if (!cp || *cp == '\0') {
+		iput(bprm->inode);
 		return -ENOEXEC; /* No interpreter name found */
+	}
 	interp = i_name = cp;
 	i_arg = 0;
 	for ( ; *cp && (*cp != ' ') && (*cp != '\t'); cp++) {
@@ -51,11 +56,60 @@
 	if (*cp)
 		i_arg = cp;
 	/*
-	 * OK, we've parsed out the interpreter name and
-	 * (optional) argument.
+	 * OK, we've parsed out the interpreter name and (optional) argument.
+	 * Now open a file descriptor to the script file if necessary for
+	 * secure set-id purposes.
+	 */
+	if (bprm->e_uid != current->euid ||
+	    bprm->e_gid != current->egid) {
+	    struct file *f;
+	    int error;
+	    f = get_empty_filp();
+	    if (!f) {
+		iput(bprm->inode);
+		return -ENFILE;
+	    }
+
+	    f->f_flags = 0;
+	    f->f_mode = FMODE_READ;
+	    f->f_inode = bprm->inode;
+	    f->f_pos = 0;
+	    f->f_reada = 0;
+	    f->f_op = NULL;
+	    if (f->f_inode->i_op)
+		f->f_op = f->f_inode->i_op->default_file_ops;
+	    if (f->f_op && f->f_op->open) {
+		error = f->f_op->open(f->f_inode, f);
+		if (error) {
+		    f->f_count--;
+		    iput(bprm->inode);
+		    return error;
+		}
+	    }
+
+	    script_fd = get_unused_fd();
+	    if (script_fd < 0) {
+		close_fp(f);
+		return script_fd;
+	    }
+	    current->files->fd[script_fd] = f;
+	    sprintf(buf, "/dev/fd/%d", script_fd);
+	    bprm->filename = buf;
+
+printk("set-ID interpreter exec: ");
+if (i_arg)
+printk("intp=%s, arg1=%s, arg2=%s\n", i_name, i_arg, bprm->filename);
+else
+printk("intp=%s, arg1=%s\n", i_name, bprm->filename);
+	} else {
+	    iput(bprm->inode);
+	    script_fd = -1;
+	}
+
+	/*
 	 * Splice in (1) the interpreter's name for argv[0]
 	 *           (2) (optional) argument to interpreter
-	 *           (3) filename of shell script (replace argv[0])
+	 *           (3) filename of script or /dev/fd/N (replace argv[0])
 	 *
 	 * This is done in reverse order, because of how the
 	 * user environment and arguments are stored.
@@ -69,21 +123,51 @@
 	}
 	bprm->p = copy_strings(1, &i_name, bprm->page, bprm->p, 2);
 	bprm->argc++;
-	if (!bprm->p) 
-		return -E2BIG;
+	if (!bprm->p) {
+	    if (script_fd>=0) {
+		struct file *f = current->files->fd[script_fd];
+		current->files->fd[script_fd] = 0;
+		put_unused_fd(script_fd);
+		close_fp(f);
+	    }
+	    return -E2BIG;
+	}
+
 	/*
 	 * OK, now restart the process with the interpreter's inode.
 	 * Note that we use open_namei() as the name is now in kernel
 	 * space, and we don't need to copy it.
 	 */
 	retval = open_namei(interp, 0, 0, &bprm->inode, NULL);
-	if (retval)
-		return retval;
-	bprm->dont_iput=0;
-	retval=prepare_binprm(bprm);
-	if(retval<0)
-		return retval;
-	return search_binary_handler(bprm,regs);
+	if (retval) {
+	    if (script_fd>=0) {
+		struct file *f = current->files->fd[script_fd];
+		current->files->fd[script_fd] = 0;
+		put_unused_fd(script_fd);
+		close_fp(f);
+	    }
+	    return retval;
+	}
+	bprm->dont_iput = 0;
+	retval = prepare_binprm(bprm);
+	if (retval<0) {
+	    if (script_fd>=0) {
+		struct file *f = current->files->fd[script_fd];
+		current->files->fd[script_fd] = 0;
+		put_unused_fd(script_fd);
+		close_fp(f);
+	    }
+	    return retval;
+	}
+
+	retval = search_binary_handler(bprm, regs);
+	if (retval<0 && script_fd>=0) {
+	    struct file *f = current->files->fd[script_fd];
+	    current->files->fd[script_fd] = 0;
+	    put_unused_fd(script_fd);
+	    close_fp(f);
+	}
+	return retval;
 }
 
 static int load_script(struct linux_binprm *bprm,struct pt_regs *regs)


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: SUID scripts?
  1998-08-07  0:23   ` Mike "Ford" Ditto
@ 1998-08-07  4:03     ` Michal Jaegermann
  1998-08-07 11:29       ` cfm
  0 siblings, 1 reply; 6+ messages in thread
From: Michal Jaegermann @ 1998-08-07  4:03 UTC (permalink / raw)
  To: axp-list

Mike \"Ford\" Ditto wrote:
> 
> Here is an implementation of "secure" set-ID #! files (A.K.A. set-uid
> shell scripts) for Linux 2.0.x.

[ Long kernel hack with unknown security implications and a need to
  be redone for every new kernel version elided... ]

Instead of that it is much simpler to have a compiled "wrapper"
program which can call your shell script, avoiding various race
conditions, and execute it under id you want.  This is, for example,
what I use to run various font making scripts for TeX, under
a special id 'tex' and not 'root', on machines when I do not want
to leave all TeX directory trees world writable.  It can be easily
adapted for these few situations when you **really** want to run
a shell script under some other id.

It is called under a desired name and executes "namesake" not
in your PATH.

   Michal

/****************************************************/
/*                                                  */
/*  Executable wrapper for MakeTeX... programs.     */
/*  Calls its namesake from TOOLS directory.        */
/*  Provide links with different names to make it   */
/*  multipurpose.                                   */
/*                                                  */
/*  Michal Jaegermann, Feb 11 1995                  */
/*                                                  */
/****************************************************/

#include <unistd.h>
#include <string.h>
#include <stdlib.h>

/* Where are real scripts located */
#define TOOLS "/usr/lib/texmf/bin/i686-linux/"
#define ASIZE  120

/*
 * This is a list of names under which we are willing
 * to execute. It has be NULL terminated.
 */
const char *accepted[] = {
    "MakeTeXPK",
    "MakeTeXTFM",
    "MakeTeXMF",
    "MakeTeXmkdir",
    NULL
};

int
main(int argc, char **argv)
{
    char doer[ASIZE] = TOOLS;
    int idx = 0;
    /*
     * If your compiler is broken and the construction below
     * does not work then "tail = strchr(doer, '\0');", or
     * equivalent, will serve as well.
     */
    char *tail = doer + (sizeof(TOOLS) - 1);
    char *start;

    /* find our base name */
    start = (start = strrchr(argv[0], '/')) ?
                               (start + 1) : argv[0];
    /* check if we are on the list */
    while (1) {
	if (NULL == accepted[idx])
	    exit(1);          /* not on the list - bye-bye */
	if (0 == strcmp(accepted[idx], start))
	    break;            /* this is ours */
	idx += 1;
    }
    /*
     * Set pretty bland, but hopefuly secure environment;
     * we intend to run this program 'suid'.
     */
    setenv("PATH", "/usr/local/bin:/bin:/usr/bin", 1);
    setenv("IFS", " ", 1);
    /*
     * You may want/need some other calls to setenv().
     * For example, if your system has an environment
     * variable pointing to shared libraries it should
     * be set here.
     */

    /*
     * Attach our name at the end of a directory string.
     * This assumes that real scripts in TOOLS directory
     * will be called by their own names (but indirectly)
     */
    strncpy(tail, start, sizeof("MakeTeXmkdir" + 1));
    setuid(geteuid());		/* we want priviledges of
				   an owner of this program */
    return execv(doer, argv);   /* do it and tell results   */
}

/*
 * Local variables:
 * compile-command: "gcc -s -O6 -fomit-frame-pointer -m486 -Wall -pipe -o ../MakeTeXPK maketex.c"
 * End:
 */



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: SUID scripts?
  1998-08-07  4:03     ` Michal Jaegermann
@ 1998-08-07 11:29       ` cfm
  1998-08-08 10:55         ` Christoph Zens
  0 siblings, 1 reply; 6+ messages in thread
From: cfm @ 1998-08-07 11:29 UTC (permalink / raw)
  To: axp-list; +Cc: axp-list

> Mike \"Ford\" Ditto wrote:
> > 
> > Here is an implementation of "secure" set-ID #! files (A.K.A. set-uid
> > shell scripts) for Linux 2.0.x.
> 
> [ Long kernel hack with unknown security implications and a need to
>   be redone for every new kernel version elided... ]
> 
> Instead of that it is much simpler to have a compiled "wrapper"
> program which can call your shell script, avoiding various race
> conditions, and execute it under id you want.  This is, for example,

Just a thought: even that might not always work with every
shell.  I recently built bash 2.latest and had to change some 
default setting that had to do with this.  ( It is documented 
in the bash README.)  If I understand correctly, the `full-featured'
shells like bash aren't appropriate for execution SUID and that
more, uh, rigorous shells like /bin/sh or better yet, perl are more
suited.

If you are going to wrap it and run it suid, you are probably better
off doing it in perl.

$.02

cfm


 




-- 

Christopher F. Miller, Publisher                            cfm@maine.com
MaineStreet Communications, Inc        208 Portland Road, Gray, ME  04039
1.207.657.5078  (MTRF 3-5pm)                        http://www.maine.com/


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: SUID scripts?
  1998-08-07 11:29       ` cfm
@ 1998-08-08 10:55         ` Christoph Zens
  0 siblings, 0 replies; 6+ messages in thread
From: Christoph Zens @ 1998-08-08 10:55 UTC (permalink / raw)
  To: axp-list


> > Instead of that it is much simpler to have a compiled "wrapper"
> > program which can call your shell script, avoiding various race
> > conditions, and execute it under id you want.
> 
> If you are going to wrap it and run it suid, you are probably better
> off doing it in perl.

Ever tried to use "sudo" ?

SYNOPSIS
       sudo -V | -h | -l | -v | -k | -s | -H | [ -b ] | [ -p
       prompt ] [ -u username/#uid] command

DESCRIPTION
       sudo allows a permitted user to execute a command as the
       superuser (real and effective uid and gid are set to 0 and
       root's group as set in the passwd file respectively).

       sudo determines who is an authorized user by consulting
       the file /etc/sudoers.  By giving sudo the -v flag a user
       can update the time stamp without running a command.  The
       password prompt itself will also time out if the password
       is not entered with N minutes (again, this is defined at
       installation time and defaults to 5 minutes).

And as far as other IDs than root are concerned:

       -u  The -u (user) option causes sudo to run the specified
           command as a user other than root.  To specify a uid
           instead of a username, use "#uid".

I use it to allow SOME users to execute a certain shell-script as root.

--
    _
 _ //
 \X/  Christoph Zens <e9126506@student.tuwien.ac.at>



^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~1998-08-08 10:56 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
1998-08-06 18:42 SUID scripts? Christopher Curtis
1998-08-06 18:55 ` Jeff Sturm
1998-08-07  0:23   ` Mike "Ford" Ditto
1998-08-07  4:03     ` Michal Jaegermann
1998-08-07 11:29       ` cfm
1998-08-08 10:55         ` Christoph Zens

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox