venuswu
venuswu

Reputation: 107

A usage of setuid?

I read a Powerpoint slide recently, and can't get a clear concept about the usage of setuid. Here is the content of the slide.

Setuid example - printing a file

  • Goals
    • Every user can queue files
    • Users cannot delete other users' files
  • Solution
    • Queue directory owned by user printer
    • Setuid queue-file program
      • Create queue file as user printer
      • Copy joe's data as user joe
    • Also, setuid remove-file program
      • Allows removal only of files you queued
    • User printer mediated user joe's queue access.

Here are my questions. I don't have a clear ​understanding​ how to solve this problem by "setuid".

  1. First, if the file is created by user printer, how does user joe copy data to a file owned by printer. Does it have some certain right to this file.
  2. Secondly, how to identify the relationship between you and the file you queued.
  3. Does the file's state show something, but the file is owned by printer, which can only be changed by root. Does it have something to do with the file's gid? If so, other users' gid may also be related to this file.
  4. Does the file's context show something about who queued it?

My questions are a mess, and I really don't have a clear concept about the solution.

Upvotes: 3

Views: 948

Answers (1)

Jonathan Leffler
Jonathan Leffler

Reputation: 755094

Let's assume that the files are to be stored in /var/spool/pq directory; the permissions on the directory printer:printgrp:2700 (owner printer, group printgrp, mode 2700 - read, write, execute for owner only, and the SGID bit set on the directory means all files created in it will belong to group printgrp).

Further, we assume that the print queuing program, pq, has permissions printer:printgrp:4511 (setuid printer, anyone can execute it, but only printer or root can look at it).

Now, suppose Joe runs pq /home/joe/file, and the permissions on the file are joe:staff:600 (only Joe can read or write to the file). Joe's umask is set to 022 (though this file has more restrictive permissions than are implied by the umask).

When the program runs, the real UID of the process is joe, but the effective UID is printer. Since there is no need for setgid operation here, both the real and effective GID are staff. Joe's auxilliary groups list does not include printgrp.

Note that it is the effective UID and GID of a process that control its access to files. On his own, Joe cannot see what jobs are in the printer queue; the directory permissions shown don't even allow him to list the files, or access the files in the directory. Note that the converse applies; on its own, the printer user (or the pq program running as that user) cannot access Joe's file.

The pq program can create files in the printer queue directory; it will probably create two files for this request. One will be a control file, the other will be a copy of the file to be printed. It will determine a job number by some mechanism (say 12345) and it might create and open two files for writing (with restrictive permissions - 0600 or even 0400):

  • /var/spool/pq/c.12345 - the control file
  • /var/spool/pq/d.12345.1 - the first (only) data file

It should then reset its effective UID to the real UID, so it is running as Joe. It can then open the file Joe asked to be printed, and copy it to the data file. It can also write whatever information it deems relevant to the control file (date, time, who submitted the request, the number of files to be printed, special printing options, etc). When it closes those files, Joe is no longer able to access them. But the program was able to copy Joe's file to its print queue.

So, that addresses questions 1 (permissions) and 2 (control information), and also 4 (control information again).

Regarding question 3, root is always a wild card on Unix-like systems because can do anything that he wants. However, as suggested by the rest of the answer, the control file contains information about the print request, including (in particular) who submitted it. You can use setgid programs instead of setuid programs; these work in analogous ways. However, under the system I postulated, the group permissions essentially didn't come into the picture. The pq program set the permissions on the control file and data file such that the group can't read it, and the directory permissions also deny group access.

We can postulate two more programs:

  • pqs - printer queue status
  • pqr - printer queue remove

These programs would also be setuid printer. The pqs program can read the control files in the directory and list pertinent information from them. The pqr program can read the control files to ensure that when Joe submitted job 12345 when he requests the removal of job 12345. If the program is satisfied, then it can delete the files.

Separately from these user-invoked programs, there would also be a daemon program (conventionally named pqd in this system) that would be kicked into action by pq if it is not already running. It would be responsible for reading the control files in the directory, and using that information to actually print the data files to the relevant printer. The details of how different printers and different data formats are handled are a problem for the daemon to deal with. The daemon too will run with printer privileges, and printer will have been given access to the printer devices (for locally attached printers) or configured to communicate over the network with a protocol such as IPP (Internet Printer Protocol). Joe probably won't be able to use the printer devices directly.

Note that the setuid programs have powers that Joe does not have. They must be written carefully to ensure that Joe cannot abuse those extra powers. Any setuid program is somewhat dangerous; a setuid root program can be lethal. In general, setgid programs are less dangerous. However, for both types of program, great care is required in the writing of such programs.

Upvotes: 3

Related Questions