suid/sudo in python

Ben Finney ben+python at benfinney.id.au
Mon Mar 30 03:06:58 EDT 2009


rustom <rustompmody at gmail.com> writes:

> Im trying to write a program that has su permissions for some file
> system tasks and is non-su elsewhere.

On Unix, ‘su’ is a program for switching to a different user; it's not
the name of a particular user. I presume you mean “ … that has root
permissions for tome file system tasks and is non-root elsewhere”.

> This is typically done in C with suid root owned code.

“suid root” refers to setting the program file with a specific mode
flag on the filesystem so the kernel will cause the process to have
the permissions of the owner of the file (rather than the user who ran
the program), and setting the owner of the program file to ‘root’.

It's not specific to C code (it works no matter what language the
program is written in), and is only one of many ways a program can run
as ‘root’. Perhaps the simplest way to run a program as ‘root’ is to
*be* ‘root’ before running the program.

> What is the python paradigm for this kind of thing? (if at all)

The key thing to realise is that, having relinquished privilege, the
same process can't get it back again as easily. So if you need to do
some tasks as a privileged user, do those *very* early and then drop
the privileges for the rest of the life of the process.

Taking this further, you should isolate exactly what tasks need root
privilege into a separate process altogether, and make that process as
well-tested and simple as possible: it should do nothing *but* those
tasks for which it needs root privilege.

-- 
 \      “We tend to scoff at the beliefs of the ancients. But we can't |
  `\        scoff at them personally, to their faces, and this is what |
_o__)                                         annoys me.” —Jack Handey |
Ben Finney



More information about the Python-list mailing list