Click to See Complete Forum and Search --> : allow a non-root user to run a program properly -> ioperm and outb


Cerf
05-31-2004, 05:19 PM
Yo yo yo

I made a program in C++ to controll the parallel port (I finally got it right).


#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>
#include <unistd.h>
#include <sys/io.h>

#define BASEPORT 0x378 /* lp1 */

int main()
{
/* Get access to the ports */
if (ioperm(BASEPORT, 3, 1))
{
perror("ioperm");
}

for (int i = 0; i < 256; i++)
{
outb(i, BASEPORT);
usleep(10000);
}

/* Read from the status port (BASE+1) and display the result */
// printf("status: %d\n", inb(BASEPORT + 1));

/* We don't need the ports anymore */
if (ioperm(BASEPORT, 3, 0))
{
perror("ioperm");
}
}


When I run the program as root the program works fine, when not as root, I have a problem.


[Cerf@localhost parallelport]$ ./parallelport
ioperm: Operation not permitted
Segmentation fault
[Cerf@localhost parallelport]$ su
Password:
[root@localhost parallelport]# ./parallelport
[root@localhost parallelport]#

Strogian
05-31-2004, 05:43 PM
if (ioperm(BASEPORT, 3, 1))
{
perror("ioperm");
}

ok first, perror doesn't quit the program, so you'll want to make it something like this:

if (ioperm(BASEPORT, 3, 1))
{
perror("ioperm");
exit(1);
}

That'll get rid of the segfault, but it still won't work. :)

to make it work, you either need to be root, the program needs to be suid root, or you can probably use something like 'sudo', which i've never used.

maccorin
05-31-2004, 05:52 PM
you want setuid root

as root


chmod 4755 the_program


you may need to setuid(0) (man 2) in the beginning of the program, some systems run it as a user until it calls setuid...

bwkaz
05-31-2004, 09:13 PM
ioperm() is a privileged function: non-root users cannot use it. If they were able to use it, they could rewrite the entire disk (or any portion of it) without permission, by interfacing with the low-level IDE hardware directly. This is obviously a Bad Thing.

Cerf
05-31-2004, 10:01 PM
Originally posted by maccorin
you want setuid root

as root


chmod 4755 the_program


you may need to setuid(0) (man 2) in the beginning of the program, some systems run it as a user until it calls setuid...

I compiled the program as root and did a chmod 4777 - mabie not the safest thing but it works for me:)

Thanks for the help