-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Expand file tree
/
Copy pathposix_spawn.cpp
More file actions
277 lines (229 loc) Β· 11 KB
/
posix_spawn.cpp
File metadata and controls
277 lines (229 loc) Β· 11 KB
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/*
* Copyright (c) 2025, TomΓ‘s SimΓ΅es <tomasprsimoes@tecnico.ulisboa.pt>
* Copyright (c) 2026, FΔ±rat KΔ±zΔ±lboΔa <firatkizilboga11@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <Kernel/API/spawn.h>
#include <Kernel/Debug.h>
#include <Kernel/Devices/BaseDevices.h>
#include <Kernel/Devices/Device.h>
#include <Kernel/Devices/Generic/NullDevice.h>
#include <Kernel/Devices/TTY/TTY.h>
#include <Kernel/FileSystem/Custody.h>
#include <Kernel/FileSystem/VirtualFileSystem.h>
#include <Kernel/Memory/Region.h>
#include <Kernel/Net/LocalSocket.h>
#include <Kernel/Tasks/PerformanceManager.h>
#include <Kernel/Tasks/Process.h>
#include <Kernel/Tasks/Scheduler.h>
#include <Kernel/Tasks/ScopedProcessList.h>
namespace Kernel {
ErrorOr<void> Process::execute_file_actions(ReadonlyBytes file_actions_data)
{
size_t offset = 0;
while (offset < file_actions_data.size()) {
if (offset + sizeof(SpawnFileActionHeader) > file_actions_data.size())
return EINVAL;
auto const* header = reinterpret_cast<SpawnFileActionHeader const*>(file_actions_data.data() + offset);
if (header->record_length < sizeof(SpawnFileActionHeader))
return EINVAL;
if (offset + header->record_length > file_actions_data.size())
return EINVAL;
switch (header->type) {
case SpawnFileActionType::Dup2: {
if (header->record_length < sizeof(SpawnFileActionDup2))
return EINVAL;
auto const* action = reinterpret_cast<SpawnFileActionDup2 const*>(header);
TRY(m_fds.with_exclusive([&](auto& fds) -> ErrorOr<void> {
if (action->new_fd < 0 || static_cast<size_t>(action->new_fd) >= fds.max_open())
return EINVAL;
auto description = TRY(fds.open_file_description(action->old_fd));
if (action->old_fd != action->new_fd) {
if (fds.m_fds_metadatas[action->new_fd].is_allocated()) {
if (auto* old_description = fds[action->new_fd].description())
(void)old_description->close();
fds[action->new_fd].clear();
} else {
fds.m_fds_metadatas[action->new_fd].allocate();
}
fds[action->new_fd].set(move(description));
}
return {};
}));
break;
}
case SpawnFileActionType::Close: {
if (header->record_length < sizeof(SpawnFileActionClose))
return EINVAL;
auto const* action = reinterpret_cast<SpawnFileActionClose const*>(header);
TRY(m_fds.with_exclusive([&](auto& fds) -> ErrorOr<void> {
auto description = TRY(fds.open_file_description(action->fd));
TRY(description->close());
fds[action->fd].clear();
return {};
}));
break;
}
case SpawnFileActionType::Open: {
if (header->record_length < sizeof(SpawnFileActionOpen))
return EINVAL;
auto const* action = reinterpret_cast<SpawnFileActionOpen const*>(header);
if (header->record_length < sizeof(SpawnFileActionOpen) + action->path_length)
return EINVAL;
auto path = TRY(KString::try_create(StringView { action->path, action->path_length }));
CustodyBase base(AT_FDCWD, path->view());
auto description = TRY(VirtualFileSystem::open(
vfs_root_context(), credentials(), path->view(),
action->flags, action->mode & ~umask(), base));
if (description->inode() && description->inode()->bound_socket())
return ENXIO;
TRY(m_fds.with_exclusive([&](auto& fds) -> ErrorOr<void> {
if (action->fd < 0 || static_cast<size_t>(action->fd) >= fds.max_open())
return EINVAL;
if (fds.m_fds_metadatas[action->fd].is_allocated()) {
if (auto* old_description = fds[action->fd].description())
(void)old_description->close();
fds[action->fd].clear();
} else {
fds.m_fds_metadatas[action->fd].allocate();
}
u32 fd_flags = (action->flags & O_CLOEXEC) ? FD_CLOEXEC : 0;
fds[action->fd].set(move(description), fd_flags);
return {};
}));
break;
}
case SpawnFileActionType::Chdir: {
if (header->record_length < sizeof(SpawnFileActionChdir))
return EINVAL;
auto const* action = reinterpret_cast<SpawnFileActionChdir const*>(header);
if (header->record_length < sizeof(SpawnFileActionChdir) + action->path_length)
return EINVAL;
auto path = TRY(KString::try_create(StringView { action->path, action->path_length }));
auto new_directory = TRY(VirtualFileSystem::open_directory(
vfs_root_context(), credentials(), path->view(), current_directory()));
m_current_directory.with([&](auto& current_directory) {
current_directory = move(new_directory);
});
break;
}
case SpawnFileActionType::Fchdir: {
if (header->record_length < sizeof(SpawnFileActionFchdir))
return EINVAL;
auto const* action = reinterpret_cast<SpawnFileActionFchdir const*>(header);
auto description = TRY(open_file_description(action->fd));
if (!description->is_directory())
return ENOTDIR;
// Check for search (+x) permission on the directory.
if (!description->metadata().may_execute(credentials()))
return EACCES;
m_current_directory.with([&](auto& current_directory) {
current_directory = description->custody();
});
break;
}
default:
return EINVAL;
}
offset += header->record_length;
}
return {};
}
// https://pubs.opengroup.org/onlinepubs/9799919799/functions/posix_spawn.html
ErrorOr<FlatPtr> Process::sys$posix_spawn(Userspace<Syscall::SC_posix_spawn_params const*> user_params)
{
VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this);
TRY(require_promise(Pledge::proc));
TRY(require_promise(Pledge::exec));
auto params = TRY(copy_typed_from_user(user_params));
if (params.arguments.length > ARG_MAX || params.environment.length > ARG_MAX)
return E2BIG;
if (params.arguments.length == 0)
return EINVAL;
if (params.attr_data.ptr() != 0 || params.attr_data_size != 0) {
// FIXME: Implement spawn attributes handling.
return ENOTSUP;
}
// Copy file actions buffer from userspace
OwnPtr<KBuffer> file_actions_buffer;
if (params.serialized_file_actions_data.ptr() != 0 && params.serialized_file_actions_data_size != 0) {
if (params.serialized_file_actions_data_size > 1 * MiB)
return E2BIG;
file_actions_buffer = TRY(KBuffer::try_create_with_size("posix_spawn file actions"sv, params.serialized_file_actions_data_size));
TRY(copy_from_user(file_actions_buffer->data(), Userspace<u8 const*> { params.serialized_file_actions_data.ptr() }, params.serialized_file_actions_data_size));
}
auto path = TRY(get_syscall_path_argument(params.path));
auto copy_user_strings = [](auto const& list, auto& output) -> ErrorOr<void> {
if (!list.length)
return {};
Checked<size_t> size = sizeof(*list.strings);
size *= list.length;
if (size.has_overflow())
return EOVERFLOW;
Vector<Syscall::StringArgument, 32> strings;
TRY(strings.try_resize(list.length));
TRY(copy_from_user(strings.data(), list.strings, size.value()));
for (size_t i = 0; i < list.length; ++i) {
auto string = TRY(try_copy_kstring_from_user(strings[i]));
TRY(output.try_append(move(string)));
}
return {};
};
Vector<NonnullOwnPtr<KString>> arguments;
TRY(copy_user_strings(params.arguments, arguments));
Vector<NonnullOwnPtr<KString>> environment;
TRY(copy_user_strings(params.environment, environment));
auto const& credentials = this->credentials();
VERIFY(!m_is_kernel_process);
auto [child, child_first_thread] = TRY(Process::create_spawned(credentials->uid(), credentials->gid(), pid(), vfs_root_context(), hostname_context(), current_directory(), tty()));
ArmedScopeGuard thread_finalizer_guard = [&child_first_thread]() {
SpinlockLocker lock(g_scheduler_lock);
child_first_thread->detach();
child_first_thread->set_state(Thread::State::Dying);
};
// "It is implementation-defined whether the fork handlers are run when posix_spawn() or posix_spawnp() is called."
// We don't run them, as they are currently implemented in LibC.
TRY(child->m_fds.with_exclusive([&](auto& child_fds) {
return m_fds.with_exclusive([&](auto const& parent_fds) {
return child_fds.try_clone(parent_fds);
});
// FD_CLOEXEC is handled by do_exec().
// FIXME: Support FD_CLOFORK.
}));
// Execute file actions on the child's FD table
if (file_actions_buffer) {
TRY(child->execute_file_actions(file_actions_buffer->bytes()));
}
// FIXME: "If file descriptor 0, 1, or 2 would otherwise be closed in the new process image created by posix_spawn() or posix_spawnp(),
// implementations may open an unspecified file for the file descriptor in the new process image."
// Copy protected data which isn't set by do_exec().
child->with_mutable_protected_data([&](auto& child_protected_data) {
with_protected_data([&](auto const& parent_protected_data) {
child_protected_data.umask = parent_protected_data.umask;
child_protected_data.process_group = parent_protected_data.process_group;
child_protected_data.credentials = parent_protected_data.credentials;
});
});
dbgln_if(FORK_DEBUG, "posix_spawn: child={}", child);
// A child created via posix_spawn inherits a copy of its parent's signal mask
child_first_thread->update_signal_mask(Thread::current()->signal_mask());
Thread* new_main_thread = nullptr;
InterruptsState previous_interrupts_state = InterruptsState::Enabled;
TRY(child->exec(move(path), move(arguments), move(environment), new_main_thread, previous_interrupts_state, ProcessEventType::Create));
thread_finalizer_guard.disarm();
m_scoped_process_list.with([&](auto const& list_ptr) {
if (list_ptr) {
child->m_scoped_process_list.with([&](auto& child_list_ptr) {
child_list_ptr = list_ptr;
});
list_ptr->attach(*child);
}
});
commit_creation(child);
SpinlockLocker lock(g_scheduler_lock);
new_main_thread->set_affinity(Thread::current()->affinity());
new_main_thread->set_state(Thread::State::Runnable);
return child->pid().value();
}
}