LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Security
User Name
Password
Linux - Security This forum is for all security related questions.
Questions, tips, system compromises, firewalls, etc. are all included here.

Notices


Reply
  Search this Thread
Old 06-08-2006, 01:23 AM   #1
Calltor
Member
 
Registered: Aug 2005
Location: Jerusalem, Israel
Distribution: Slackware 10.1
Posts: 42

Rep: Reputation: 15
PHP exec() on Apache - Privileges Problem


Hi all,

I'm running Apache server on my machine, and I am unable to execute command that need root privileges, in this case running a program that writes to the parallel port. The program runs fine from the terminal with sudo or when logged in as root.

When I run on the server echo exec('whoami'), I get "nobody". The server was started as root.

Is there any way I can solve this problem using sudo? I need to pass the password for sudo within the exec() command, inside a PHP page.

Thanks
 
Old 06-08-2006, 05:08 AM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
I need to pass the password for sudo within the exec() command, inside a PHP page
AFAIK not unless you set up a proper Sudo Cmnd_Alias (make sure you scrub everything for parameter abuse) and give it to the user as a NOPASSWD entry.
 
Old 06-08-2006, 05:20 AM   #3
nx5000
Senior Member
 
Registered: Sep 2005
Location: Out
Posts: 3,307

Rep: Reputation: 57
There is this:
http://www.suphp.org/Home.html
I haven't tried it but it seems stable as others are using it.
 
Old 06-08-2006, 06:18 AM   #4
penguintutor
Member
 
Registered: Jun 2006
Location: UK
Distribution: Ubuntu, Mandriva, Redhat and Fedora
Posts: 118

Rep: Reputation: 15
A possible solution, depending upon your system would be to give the required privileges to the nobody user.

E.g. adding the user "nobody" to the "lp" group, which will normally have write permission to the parallel port.

e.g.
First check if there are any other groups that nobody is currently part of using the groups command. If there are then add these to a comma separated list as well as the lp group (although no need to include the group nobody which should be the users default group).
usermod -G lp nobody

Depending upon what your program is doing it may not be that simple, but can work for some purposes.

A similar technique is used to give normal users access to the floppy drive, which is normally handled by default by most distros.

If it works this is more secure than allowing a webserver to run commands as root.
 
Old 06-08-2006, 06:53 AM   #5
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
OK. Can someone please do me a favour and explain the pro's and cons of these three options?
 
Old 06-08-2006, 07:15 AM   #6
Calltor
Member
 
Registered: Aug 2005
Location: Jerusalem, Israel
Distribution: Slackware 10.1
Posts: 42

Original Poster
Rep: Reputation: 15
Thanks to all of you

and btw, I don't care about security this time... all this machine has to do is to control the parallel port it's a 400Mhz Celeron...
 
Old 06-08-2006, 07:32 AM   #7
nx5000
Senior Member
 
Registered: Sep 2005
Location: Out
Posts: 3,307

Rep: Reputation: 57
Different solutions, pros/cons:
-> basic sudo
pros:fine grain on which user is able to execute which executable
cons: possbility to use this feature out of apache. Not really a cons as far as I can see, depends on how everything is done.

->suphp
From what I read on their website, you configure suphp to run one php script with the privilege of a choosen user.
pros:can do a chroot, has extensive checks.
cons:you bring the suphp suid binary (not your php script) on your machine. you can couple it with sudo for a certain user if you don't like having suid on your machine runnable by everyone. Also you have to audit the complete suphp mechanism compared to sudo which is very small/tested.

->nobody is put in lp group
pros:very easy to implement
cons:every process that you can control execution (by means of a vuln or a self-made binary sent to the server) running in user nobody will be able to print hundred of pages with "fuc* off" on your printer (nice idea)

->run apache as root
pros: "no security for now", very quick
cons: your machine will be severly compromised in case of a vulnerability.

That's a very quick answer. Discussion is opened..
Cheers

ps: I prefer the method of sudo from unspawn I think , with a chrooted apache

Last edited by nx5000; 06-08-2006 at 07:34 AM.
 
Old 06-08-2006, 11:13 AM   #8
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
@Calltor: I don't care about security
...so it apparently makes sense we all do, and so should you. Coding something w/o regard for security is a recipe for disaster, security ain't no "add-on", besides with anyone using PHP I'm twice as suspicious.


@NX5000: good comparison, WD. I agree introducing "extra" setuid binaries ain't a good start if you can avoid it, and you make a good point about chrooting. I fear the real problem starts with interaction between parts: scrubbing user input (which should never be trusted) and restrict supplying parameters. Sudo can restrict which arguments are allowed, but the "app" in front should do all the work essentially.

Somehow I get the idea we're stuck with another hit 'n run who will never read this...
 
Old 06-09-2006, 06:29 AM   #9
Calltor
Member
 
Registered: Aug 2005
Location: Jerusalem, Israel
Distribution: Slackware 10.1
Posts: 42

Original Poster
Rep: Reputation: 15
I've said "I don't care about security this time", and not " I don't care about security at all", and this is because the server is accessiable only in my LAN, and all it has to do is to control some parallel port device I made. I do care about security when there is some reason to
 
Old 06-09-2006, 06:40 AM   #10
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Ubuntu 12.04, Antix19.3
Posts: 3,794

Rep: Reputation: 282Reputation: 282Reputation: 282
A different approach that might work: make use of setuid in your program. This however can only be done if you can modify the source of the program.

probably the best example of a program that uses setuid is probably ping, which needs root privileges but can be run by any ordinary user.
 
Old 06-09-2006, 06:42 AM   #11
Calltor
Member
 
Registered: Aug 2005
Location: Jerusalem, Israel
Distribution: Slackware 10.1
Posts: 42

Original Poster
Rep: Reputation: 15
Of course, I wrote the program. Can you plz tell me how to use setuid?
 
Old 06-09-2006, 06:56 AM   #12
Calltor
Member
 
Registered: Aug 2005
Location: Jerusalem, Israel
Distribution: Slackware 10.1
Posts: 42

Original Poster
Rep: Reputation: 15
This is my script now, but again, I can only run it as root...

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <asm/io.h>

#define port 0x378

main(int argc, char **argv){
int value;

setuid(0);

if(argc != 2)
fprintf(stderr, "Error: Wrong number of arguments\n"), exit(1);
if(sscanf(argv[1], "%i", &value) != 1)
fprintf(stderr, "Error: Parameter is not a number\n"), exit(1);
if(value < 0 || value > 255)
fprintf(stderr, "Error: Invalid numeric value\n"), exit(1);
if(ioperm(port, 1, 1))
fprintf(stderr, "Error accessing port\n"), exit(1);
outb((unsigned char) value, port);
fprintf(stdout, "Data sent to port\n");
}
 
Old 06-09-2006, 08:40 AM   #13
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Ubuntu 12.04, Antix19.3
Posts: 3,794

Rep: Reputation: 282Reputation: 282Reputation: 282
Code:
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>

static uid_t euid, ruid;

/* Restore the effective UID to its original value. */
void do_setuid (void)
{
int status;

#ifdef _POSIX_SAVED_IDS
    status = seteuid (euid);
#else
    status = setreuid (ruid, euid);
#endif
    if (status < 0)
    {
        fprintf (stderr, "Couldn't set uid.\n");
        exit(status);
    }
}

/* Set the effective UID to the real UID. */
void undo_setuid (void)
{
int status;

#ifdef _POSIX_SAVED_IDS
    status = seteuid (ruid);
#else
    status = setreuid (euid, ruid);
#endif
    if (status < 0)
    {
        fprintf (stderr, "Couldn't set uid.\n");
        exit (status);
    }
}

int main()
{
FILE *fp;

    ruid = getuid ();
    euid = geteuid ();

    undo_setuid ();
    printf("uid=%d\n",getuid());
    printf("euid=%d\n",geteuid());

    fp=fopen("/var/log/debug","r");
    if(fp==NULL)
    {
        printf("Could not open file\n");
    }
    else
    {
        printf("File opened\n");
        fclose(fp);
    }

    do_setuid ();
    printf("uid=%d\n",getuid());
    printf("euid=%d\n",geteuid());

    fp=fopen("/var/log/debug","r");
    if(fp==NULL)
    {
        printf("Could not open file\n");
    }
    else
    {
        printf("File opened\n");
        fclose(fp);
    }

    return 0;

}
Compile the file, make root the owner (chown) and change the permissions
Code:
root@btd-techweb01:/home/wim/progs/lq452660# ls -l
total 20
-rwxr-xr-x  1 wim develop 12361 2006-06-09 15:33 lq452660
-rw-r--r--  1 wim develop  1381 2006-06-09 15:28 lq452660.c
root@btd-techweb01:/home/wim/progs/lq452660# chown root lq452660
root@btd-techweb01:/home/wim/progs/lq452660# chmod 4755 lq452660
root@btd-techweb01:/home/wim/progs/lq452660# ls -l
total 20
-rwsr-xr-x  1 root develop 12361 2006-06-09 15:33 lq452660
-rw-r--r--  1 wim  develop  1381 2006-06-09 15:28 lq452660.c
Run the program as normal user. It attempts to open /var/log/debug which is something that a normal user normally can't do. The first attempt will fail because we canceled 'our' special permission.
Next we re-assign it and the result will be ok.
Code:
wim@btd-techweb01:~/progs/lq452660$ ./lq452660
uid=1000
euid=1000
Could not open file
uid=1000
euid=0
File opened
The two functions at the top are taken from http://www.gnu.org/software/libc/man...m-Example.html

Please note:
Never keep the special permissions in your program longer than required. So assign just before you do something and immediately revoke again. That's also why it was initially revoked (in the beginning of the program).

Last edited by Wim Sturkenboom; 06-09-2006 at 08:43 AM.
 
Old 06-09-2006, 09:04 AM   #14
Calltor
Member
 
Registered: Aug 2005
Location: Jerusalem, Israel
Distribution: Slackware 10.1
Posts: 42

Original Poster
Rep: Reputation: 15
thank u very very much!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
php: exec can't run lynx after apache restart BrianK Programming 4 01-16-2006 05:47 PM
php exec() function in chrooted apache clau_bolson Linux - Software 2 12-21-2005 08:29 AM
PHP / VideoLAN / Fedora Core Question - how can I get the PHP "exec" function to work gtrawoger Linux - Software 3 12-21-2005 06:51 AM
php exec devit Programming 4 04-08-2004 03:26 AM
PHP exec Spooky Programming 1 01-19-2004 08:34 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Security

All times are GMT -5. The time now is 05:15 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration