1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
|
/*
* Copyright (c) 2021 Jesse Buhagiar <jooster669@gmail.com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <AK/String.h>
#include <AK/StringBuilder.h>
#include <AK/Types.h>
#include <AK/Vector.h>
#include <LibCore/Account.h>
#include <LibCore/ArgsParser.h>
#include <LibCore/File.h>
#include <LibCore/GetPassword.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
// Function Definitions
extern "C" int main(int arch, char** argv);
int unveil_paths(const char*);
// Unveil paths, given the current user's path and the command they want to execute
int unveil_paths(const char* command)
{
// Get the system path, split it and attempt to unveil all the paths.
// We do NOT error out on an invalid path
auto paths = String(getenv("PATH")).split(':');
int num_unveils = 0;
char path_buf[256];
// Unveil each path
for (const auto& path : paths) {
if (unveil(path.characters(), "x") == 0)
num_unveils++;
}
// Now unveil the command
auto command_path = realpath(command, &path_buf[0]);
if (command_path) {
if (unveil(command_path, "x") == 0)
num_unveils++;
}
return num_unveils;
}
// <kling> linusg: quaker: "please" feels quite long, how about "pls" :P
// <kling> "pls rm -r crap" has a nice ring to it
// lol
int main(int argc, char** argv)
{
Vector<const char*> command;
Core::ArgsParser args_parser;
args_parser.add_positional_argument(command, "Command to run at elevated privilege level", "command");
args_parser.parse(argc, argv);
if (pledge("stdio tty rpath exec id", nullptr) < 0) {
perror("pledge");
return 1;
}
if (seteuid(0) < 0) {
perror("seteuid");
return 1;
}
if (unveil("/etc/sudoers", "r") < 0) {
perror("unveil");
return 1;
}
if (unveil("/etc/passwd", "r") < 0) {
perror("unveil");
return 1;
}
if (unveil("/etc/shadow", "r") < 0) {
perror("unveil");
return 1;
}
if (unveil("/etc/group", "r") < 0) {
perror("unveil");
return 1;
}
// Unveil all paths in the user's PATH, as well as the command they've specified.
auto unveil_count = unveil_paths(command.at(0));
if (unveil_count == 0) {
warnln("Failed to unveil paths!");
return 1;
}
// Lock veil
unveil(nullptr, nullptr);
const char* username = getlogin();
auto sudoers_file_or_error = Core::File::open("/etc/sudoers", Core::IODevice::ReadOnly);
bool user_found = false;
if (sudoers_file_or_error.is_error()) {
warnln("couldn't open /etc/sudoers!");
return 1;
}
for (auto line = sudoers_file_or_error.value()->line_begin(); !line.at_end(); ++line) {
auto line_str = *line;
// Skip any comments
if (line_str.starts_with("#"))
continue;
// Our user is in the sudoers file!
if (line_str.to_string() == username) {
user_found = true;
break;
}
}
// User isn't in the sudoer's file
if (!user_found) {
warnln("{} is not in the sudoers file!", username);
return 2;
}
// The user was in the sudoers file, now let's ask for their password to ensure that it's actually them...
uid_t current_uid = getuid();
auto account_or_error = (username)
? Core::Account::from_name(username)
: Core::Account::from_uid(current_uid);
if (account_or_error.is_error()) {
warnln("Core::Account::from_name: {}", account_or_error.error());
return 1;
}
const auto& account = account_or_error.value();
if (current_uid != 0 && account.has_password()) {
auto password = Core::get_password();
if (password.is_error()) {
warnln("{}", password.error());
return 1;
}
if (!account.authenticate(password.value().characters())) {
warnln("Incorrect or disabled password.");
return 1;
}
}
// TODO: Support swapping users instead of just defaulting to root
setgid(0);
setuid(0);
// Build the arguments list passed to `execvpe`
Vector<const char*> exec_args;
for (size_t i = 0; i < command.size(); i++) {
exec_args.append(command.at(i));
}
// Always terminate with a NULL (to signal end of args list)
exec_args.append(nullptr);
// Build the environment arguments
StringBuilder builder;
// Build SUDO_USER envvar
builder.append("SUDO_USER=");
builder.append(username);
auto sudo_user = builder.build();
builder.clear();
// Build SUDO_COMMAND envvar
builder.append("SUDO_COMMAND=");
builder.append(command.at(0));
auto sudo_command = builder.build();
builder.clear();
const char* envs[] = { "PROMPT=\\X\\u@\\h:\\w\\a\\e[33;1m\\h\\e[0m \\e[34;1m\\w\\e[0m \\p ",
"TERM=xterm", "PAGER=more", "PATH=/bin:/usr/bin:/usr/local/bin",
sudo_user.characters(), sudo_command.characters(), nullptr };
// Execute the desired command
int rc = execvpe(command.at(0), const_cast<char**>(exec_args.data()), const_cast<char**>(envs));
if (rc < 0) {
perror("execvpe");
exit(1);
}
return 0;
}
|