Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(61)

Side by Side Diff: third_party/crashpad/crashpad/util/posix/drop_privileges.cc

Issue 1505213004: Copy Crashpad into the Chrome tree instead of importing it via DEPS (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Update Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2014 The Crashpad Authors. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include <unistd.h>
16
17 #include "base/logging.h"
18 #include "build/build_config.h"
19
20 namespace crashpad {
21
22 void DropPrivileges() {
23 gid_t gid = getgid();
24 uid_t uid = getuid();
25
26 #if defined(OS_MACOSX)
27 // Based on the POSIX.1-2008 2013 edition documentation for setreuid() and
28 // setregid(), setreuid() and setregid() alone should be sufficient to drop
29 // privileges. The standard specifies that the saved ID should be set to the
30 // effective ID whenever the real ID is not -1, or whenever the effective ID
31 // is set not equal to the real ID. This code never specifies -1, so the
32 // setreuid() and setregid() alone should work according to the standard.
33 //
34 // In practice, on Mac OS X, setuid() and setgid() (or seteuid() and
35 // setegid()) must be called first. Otherwise, setreuid() and setregid() do
36 // not alter the saved IDs, leaving open the possibility for future privilege
37 // escalation.
38 //
39 // The problem exists in 10.9.5 xnu-2422.115.4/bsd/kern/kern_prot.c
40 // setreuid(). Based on its comments, it purports to set the svuid to the new
41 // euid when the old svuid doesn’t match one of the new ruid and euid. This
42 // isn’t how POSIX.1-2008 says it should behave, but it should work for this
43 // function’s purposes. In reality, setreuid() doesn’t even do this: it sets
44 // the svuid to the old euid, which does not drop privileges when the old euid
45 // is different from the desired euid. The workaround of calling setuid() or
46 // seteuid() before setreuid() works because it sets the euid so that by the
47 // time setreuid() runs, the old euid is actually the value that ought to be
48 // set as the svuid. setregid() is similar. This bug is filed as radar
49 // 18987552.
50 //
51 // setuid() and setgid() alone will only set the saved IDs when running as
52 // root. When running a setuid non-root or setgid program, they do not alter
53 // the saved ID, and do not effect a permanent privilege drop.
54 gid_t egid = getegid();
55 PCHECK(setgid(gid) == 0) << "setgid";
56 PCHECK(setregid(gid, gid) == 0) << "setregid";
57
58 uid_t euid = geteuid();
59 PCHECK(setuid(uid) == 0) << "setuid";
60 PCHECK(setreuid(uid, uid) == 0) << "setreuid";
61
62 if (uid != 0) {
63 // Because the setXid()+setreXid() interface to change IDs is fragile,
64 // ensure that privileges cannot be regained. This can only be done if the
65 // real user ID (and now the effective user ID as well) is not root, because
66 // root always has permission to change identity.
67 if (euid != uid) {
68 CHECK_EQ(seteuid(euid), -1);
69 }
70 if (egid != gid) {
71 CHECK_EQ(setegid(egid), -1);
72 }
73 }
74 #elif defined(OS_LINUX)
75 PCHECK(setresgid(gid, gid, gid) == 0) << "setresgid";
76 PCHECK(setresuid(uid, uid, uid) == 0) << "setresuid";
77
78 // Don’t check to see if privileges can be regained on Linux, because on
79 // Linux, it’s not as simple as ensuring that this can’t be done if non-root.
80 // Instead, the ability to change user and group IDs are controlled by the
81 // CAP_SETUID and CAP_SETGID capabilities, which may be granted to non-root
82 // processes. Since the setresXid() interface is well-defined, it shouldn’t be
83 // necessary to perform any additional checking anyway.
84 //
85 // TODO(mark): Drop CAP_SETUID and CAP_SETGID if present and non-root?
86 #else
87 #error Port this function to your system.
88 #endif
89 }
90
91 } // namespace crashpad
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698