linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Luciano Miguel Ferreira Rocha <luciano@lsd.di.uminho.pt>
To: "Lejanson C. Go" <lejanson@ntsp.nec.co.jp>
Cc: linux-c-programming@vger.kernel.org
Subject: Re: ioperm() and setuid()
Date: Mon, 13 Oct 2003 14:42:56 +0100	[thread overview]
Message-ID: <20031013134256.GA24603@lsd.di.uminho.pt> (raw)
In-Reply-To: <3F89EB91.7070509@hq.ntsp.nec.co.jp>

On Mon, Oct 13, 2003 at 08:02:25AM +0800, Lejanson C. Go wrote:
> Hello,
> 
> Does anyone know how to allow non root users in linux to
> access to i/o ports using ioperm() and setuid().

I think you're misunderstanding setuid(). For a program to have I/O access,
it needs root permissions.

For a normal user to gain those permissions, the executable needs to be
owned by root and have the set-user-id bit set.

Or you can use the userhelper package to give temporary root permissions
to the user for that program.

Here's code I use for that purpose (changed a little):

int main(int ac, char *av[])
{

        /* parse and check args */
        if (parse(ac, av))
                return 1;
        /* check if enough privileges for io ops */
        if (geteuid()) {
                if (!helped)
                        /* haven't tried consolehelper yet, try it */
                        use_ch(ac, av);
                fprintf(stderr, "%s: must run with superuser privileges\n",
                        pname);
                return 1;
        }
        /* get privileges for out|in_p delay */
        if (ioperm(0x80, 1, 1)) {
                err("port 0x80 (pause)");
                return 1;
        }
        /* get privileges for out|in to lp ports */
        if (ioperm(port, IORANGE, 1)) {
                err2("port %#03x", port);
                return 1;
        }
	...
}

/* not running with root privileges, try consolehelper */
void use_ch(int oac, char *oav[])
{
        extern char **environ;
        char **nav;
                                                                                
        if (!(nav = calloc(oac + 2, sizeof(char *)))) {
                err("new args");
                return;
        }
        /* copy old args */
        memcpy(nav, oav, oac * sizeof(char *));
        /* args = args + "-c" */
        nav[oac] = "-c";
        execve("/usr/bin/consolehelper", nav, environ);
        err("consolehelper");
}

Regards,
Luciano Rocha

      parent reply	other threads:[~2003-10-13 13:42 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-10-13  0:02 ioperm() and setuid() Lejanson C. Go
2003-10-13 13:11 ` Chris Nanakos
2003-10-13 13:42 ` Luciano Miguel Ferreira Rocha [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20031013134256.GA24603@lsd.di.uminho.pt \
    --to=luciano@lsd.di.uminho.pt \
    --cc=lejanson@ntsp.nec.co.jp \
    --cc=linux-c-programming@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).