Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Usually it's a mistake to test if the user is root. POSIX does not even require a root user, but leaves it to the implementation to determine how permissions work. Code such as:</p> <pre><code>if (i_am_root) do_privileged_op(); else print_error(); </code></pre> <p>will really annoy users with advanced privilege models where root is not necessary to perform the necessary privileged operations. I remember back in the early days of cd burning on Linux, I had to hack all over the <code>cdrecord</code> source to remove all the useless checks to see if it was running as root, when it worked just fine with permission to read <code>/dev/sga</code>.</p> <p>Instead, you should always <strong>attempt</strong> the privileged operation you need to perform, and check for <code>EPERM</code> or similar if it fails to notify the user that they have insufficient privileges (and perhaps should retry running as root).</p> <p>The one case where it's useful to check for root is checking if your program was invoked "suid-root". A reasonable test would be:</p> <pre><code>uid_t uid=getuid(), euid=geteuid(); if (uid&lt;0 || uid!=euid) { /* We might have elevated privileges beyond that of the user who invoked * the program, due to suid bit. Be very careful about trusting any data! */ } else { /* Anything goes. */ } </code></pre> <p>Note that I allowed for the possibility (far-fetched, but best to be paranoid) that either of the calls to get uid/euid could fail, and that in the failure case we should assume we're suid and a malicious user has somehow caused the syscalls to fail in an attempt to hide that we're suid.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload