annotate lib/idpriv.h @ 17255:d81be792518a

update from texinfo
author Karl Berry <karl@freefriends.org>
date Tue, 01 Jan 2013 15:51:49 -0800
parents e542fd46ad6f
children 344018b6e5d7
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
11613
1d540dc13d74 New module 'idpriv-drop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1 /* Dropping uid/gid privileges of the current process.
17249
e542fd46ad6f maint: update all copyright year number ranges
Eric Blake <eblake@redhat.com>
parents: 16201
diff changeset
2 Copyright (C) 2009-2013 Free Software Foundation, Inc.
11613
1d540dc13d74 New module 'idpriv-drop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3
1d540dc13d74 New module 'idpriv-drop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4 This program is free software: you can redistribute it and/or modify
1d540dc13d74 New module 'idpriv-drop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5 it under the terms of the GNU General Public License as published by
1d540dc13d74 New module 'idpriv-drop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
6 the Free Software Foundation; either version 3 of the License, or
1d540dc13d74 New module 'idpriv-drop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
7 (at your option) any later version.
1d540dc13d74 New module 'idpriv-drop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
8
1d540dc13d74 New module 'idpriv-drop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
9 This program is distributed in the hope that it will be useful,
1d540dc13d74 New module 'idpriv-drop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
1d540dc13d74 New module 'idpriv-drop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1d540dc13d74 New module 'idpriv-drop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
12 GNU General Public License for more details.
1d540dc13d74 New module 'idpriv-drop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
13
1d540dc13d74 New module 'idpriv-drop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
14 You should have received a copy of the GNU General Public License
1d540dc13d74 New module 'idpriv-drop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
1d540dc13d74 New module 'idpriv-drop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
16
1d540dc13d74 New module 'idpriv-drop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
17 #ifndef _IDPRIV_H
1d540dc13d74 New module 'idpriv-drop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
18 #define _IDPRIV_H
1d540dc13d74 New module 'idpriv-drop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
19
1d540dc13d74 New module 'idpriv-drop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
20 #ifdef __cplusplus
1d540dc13d74 New module 'idpriv-drop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
21 extern "C" {
1d540dc13d74 New module 'idpriv-drop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
22 #endif
1d540dc13d74 New module 'idpriv-drop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
23
1d540dc13d74 New module 'idpriv-drop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
24 /* This module allows programs which are installed with setuid or setgid bit
1d540dc13d74 New module 'idpriv-drop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
25 (and which therefore initially run with an effective user id or group id
1d540dc13d74 New module 'idpriv-drop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
26 different from the one of the current user) to drop their uid or gid
1d540dc13d74 New module 'idpriv-drop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
27 privilege, either permanently or temporarily.
1d540dc13d74 New module 'idpriv-drop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
28
1d540dc13d74 New module 'idpriv-drop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
29 It is absolutely necessary to minimize the amount of code that is running
1d540dc13d74 New module 'idpriv-drop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
30 with escalated privileges (e.g. with effective uid = root). The reason is
1d540dc13d74 New module 'idpriv-drop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
31 that any bug or exploit in a part of a program that is running with
1d540dc13d74 New module 'idpriv-drop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
32 escalated privileges is a security vulnerability that - upon discovery -
1d540dc13d74 New module 'idpriv-drop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
33 puts the users in danger and requires immediate fixing. Then consider that
1d540dc13d74 New module 'idpriv-drop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
34 there's a bug every 10 or 20 lines of code on average...
1d540dc13d74 New module 'idpriv-drop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
35
1d540dc13d74 New module 'idpriv-drop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
36 For programs that temporarily drop privileges but have the ability to
1d540dc13d74 New module 'idpriv-drop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
37 restore them later, there are additionally the dangers that
1d540dc13d74 New module 'idpriv-drop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
38 - Any bug in the non-privileged part of the program may be used to
1d540dc13d74 New module 'idpriv-drop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
39 create invalid data structures that will trigger security
1d540dc13d74 New module 'idpriv-drop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
40 vulnerabilities in the privileged part of the program.
1d540dc13d74 New module 'idpriv-drop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
41 - Code execution exploits in the non-privileged part of the program may
1d540dc13d74 New module 'idpriv-drop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
42 be used to invoke the function that restores high privileges and then
1d540dc13d74 New module 'idpriv-drop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
43 execute additional arbitrary code.
1d540dc13d74 New module 'idpriv-drop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
44
1d540dc13d74 New module 'idpriv-drop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
45 1) The usual, and reasonably safe, way to minimize the amount of code
1d540dc13d74 New module 'idpriv-drop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
46 running with privileges is to create a separate executable, with setuid
1d540dc13d74 New module 'idpriv-drop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
47 or setgid bit, that contains only code for the tasks that require
1d540dc13d74 New module 'idpriv-drop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
48 privileges (and,of course, strict checking of the arguments, so that the
1d540dc13d74 New module 'idpriv-drop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
49 program cannot be abused). The main program is installed without setuid
1d540dc13d74 New module 'idpriv-drop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
50 or setgid bit.
1d540dc13d74 New module 'idpriv-drop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
51
1d540dc13d74 New module 'idpriv-drop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
52 2) A less safe way is to do some privileged tasks at the beginning of the
1d540dc13d74 New module 'idpriv-drop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
53 program's run, and drop privileges permanently as soon as possible.
1d540dc13d74 New module 'idpriv-drop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
54
1d540dc13d74 New module 'idpriv-drop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
55 Note: There may still be security issues if the privileged task puts
1d540dc13d74 New module 'idpriv-drop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
56 sensitive data into the process memory or opens communication channels
1d540dc13d74 New module 'idpriv-drop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
57 to restricted facilities.
1d540dc13d74 New module 'idpriv-drop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
58
1d540dc13d74 New module 'idpriv-drop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
59 3) The most unsafe way is to drop privileges temporarily for most of the
1d540dc13d74 New module 'idpriv-drop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
60 main program but to re-enable them for the duration of privileged tasks.
1d540dc13d74 New module 'idpriv-drop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
61
1d540dc13d74 New module 'idpriv-drop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
62 As explained above, this approach has uncontrollable dangers for
1d540dc13d74 New module 'idpriv-drop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
63 security.
1d540dc13d74 New module 'idpriv-drop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
64
1d540dc13d74 New module 'idpriv-drop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
65 This approach is normally not usable in multithreaded programs, because
1d540dc13d74 New module 'idpriv-drop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
66 you cannot know what kind of system calls the other threads could be
1d540dc13d74 New module 'idpriv-drop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
67 doing during the time the privileges are enabled.
1d540dc13d74 New module 'idpriv-drop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
68
1d540dc13d74 New module 'idpriv-drop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
69 With approach 1, you don't need gnulib modules.
1d540dc13d74 New module 'idpriv-drop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
70 With approach 2, you need the gnulib module 'idpriv-drop'.
1d540dc13d74 New module 'idpriv-drop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
71 With approach 3, you need the gnulib module 'idpriv-droptemp'. But really,
1d540dc13d74 New module 'idpriv-drop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
72 you should better stay away from this approach.
1d540dc13d74 New module 'idpriv-drop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
73 */
1d540dc13d74 New module 'idpriv-drop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
74
11620
fa0fe50fe700 Add more references.
Bruno Haible <bruno@clisp.org>
parents: 11613
diff changeset
75 /* For more in-depth discussion of these topics, see the papers/articles
fa0fe50fe700 Add more references.
Bruno Haible <bruno@clisp.org>
parents: 11613
diff changeset
76 * Hao Chen, David Wagner, Drew Dean: Setuid Demystified
fa0fe50fe700 Add more references.
Bruno Haible <bruno@clisp.org>
parents: 11613
diff changeset
77 <http://www.usenix.org/events/sec02/full_papers/chen/chen.pdf>
fa0fe50fe700 Add more references.
Bruno Haible <bruno@clisp.org>
parents: 11613
diff changeset
78 * Dan Tsafrir, Dilma da Silva, David Wagner: The Murky Issue of Changing
fa0fe50fe700 Add more references.
Bruno Haible <bruno@clisp.org>
parents: 11613
diff changeset
79 Process Identity: Revising "Setuid Demystified"
fa0fe50fe700 Add more references.
Bruno Haible <bruno@clisp.org>
parents: 11613
diff changeset
80 <http://www.eecs.berkeley.edu/~daw/papers/setuid-login08b.pdf>
fa0fe50fe700 Add more references.
Bruno Haible <bruno@clisp.org>
parents: 11613
diff changeset
81 <http://code.google.com/p/change-process-identity/>
fa0fe50fe700 Add more references.
Bruno Haible <bruno@clisp.org>
parents: 11613
diff changeset
82 * Dhruv Mohindra: Observe correct revocation order while relinquishing
fa0fe50fe700 Add more references.
Bruno Haible <bruno@clisp.org>
parents: 11613
diff changeset
83 privileges
fa0fe50fe700 Add more references.
Bruno Haible <bruno@clisp.org>
parents: 11613
diff changeset
84 <https://www.securecoding.cert.org/confluence/display/seccode/POS36-C.+Observe+correct+revocation+order+while+relinquishing+privileges>
fa0fe50fe700 Add more references.
Bruno Haible <bruno@clisp.org>
parents: 11613
diff changeset
85 */
11613
1d540dc13d74 New module 'idpriv-drop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
86
1d540dc13d74 New module 'idpriv-drop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
87
1d540dc13d74 New module 'idpriv-drop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
88 /* For approach 2. */
1d540dc13d74 New module 'idpriv-drop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
89
1d540dc13d74 New module 'idpriv-drop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
90 /* Drop the uid and gid privileges of the current process.
1d540dc13d74 New module 'idpriv-drop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
91 Return 0 if successful, or -1 with errno set upon failure. The recommended
1d540dc13d74 New module 'idpriv-drop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
92 handling of failure is to terminate the process. */
1d540dc13d74 New module 'idpriv-drop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
93 extern int idpriv_drop (void);
1d540dc13d74 New module 'idpriv-drop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
94
1d540dc13d74 New module 'idpriv-drop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
95
1d540dc13d74 New module 'idpriv-drop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
96 /* For approach 3. */
1d540dc13d74 New module 'idpriv-drop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
97
1d540dc13d74 New module 'idpriv-drop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
98 /* Drop the uid and gid privileges of the current process in a way that allows
1d540dc13d74 New module 'idpriv-drop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
99 them to be restored later.
1d540dc13d74 New module 'idpriv-drop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
100 Return 0 if successful, or -1 with errno set upon failure. The recommended
1d540dc13d74 New module 'idpriv-drop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
101 handling of failure is to terminate the process. */
1d540dc13d74 New module 'idpriv-drop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
102 extern int idpriv_temp_drop (void);
1d540dc13d74 New module 'idpriv-drop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
103
1d540dc13d74 New module 'idpriv-drop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
104 /* Restore the uid and gid privileges of the current process.
1d540dc13d74 New module 'idpriv-drop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
105 Return 0 if successful, or -1 with errno set upon failure. The recommended
1d540dc13d74 New module 'idpriv-drop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
106 handling of failure is to not perform the actions that require the escalated
1d540dc13d74 New module 'idpriv-drop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
107 privileges. */
1d540dc13d74 New module 'idpriv-drop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
108 extern int idpriv_temp_restore (void);
1d540dc13d74 New module 'idpriv-drop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
109
1d540dc13d74 New module 'idpriv-drop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
110
1d540dc13d74 New module 'idpriv-drop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
111 #ifdef __cplusplus
1d540dc13d74 New module 'idpriv-drop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
112 }
1d540dc13d74 New module 'idpriv-drop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
113 #endif
1d540dc13d74 New module 'idpriv-drop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
114
1d540dc13d74 New module 'idpriv-drop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
115
1d540dc13d74 New module 'idpriv-drop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
116 #endif /* _IDPRIV_H */