2020-07-31 00:38:15 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-07-31 00:38:15 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <Kernel/Process.h>
|
|
|
|
#include <Kernel/Random.h>
|
2020-09-12 06:11:07 +03:00
|
|
|
#include <Kernel/UserOrKernelBuffer.h>
|
2020-07-31 00:38:15 +03:00
|
|
|
|
|
|
|
namespace Kernel {
|
|
|
|
|
|
|
|
// We don't use the flag yet, but we could use it for distinguishing
|
|
|
|
// random source like Linux, unlike the OpenBSD equivalent. However, if we
|
|
|
|
// do, we should be able of the caveats that Linux has dealt with.
|
2021-11-08 02:51:39 +03:00
|
|
|
ErrorOr<FlatPtr> Process::sys$getrandom(Userspace<void*> buffer, size_t buffer_size, [[maybe_unused]] unsigned flags)
|
2020-07-31 00:38:15 +03:00
|
|
|
{
|
2021-08-06 14:43:02 +03:00
|
|
|
VERIFY_NO_PROCESS_BIG_LOCK(this);
|
2021-12-29 12:11:45 +03:00
|
|
|
TRY(require_promise(Pledge::stdio));
|
2021-06-16 17:44:15 +03:00
|
|
|
if (buffer_size > NumericLimits<ssize_t>::max())
|
2021-03-01 15:49:16 +03:00
|
|
|
return EINVAL;
|
2020-07-31 00:38:15 +03:00
|
|
|
|
2021-11-21 14:24:32 +03:00
|
|
|
auto data_buffer = TRY(UserOrKernelBuffer::for_user_buffer(buffer, buffer_size));
|
2021-09-06 03:30:27 +03:00
|
|
|
|
2021-11-21 14:24:32 +03:00
|
|
|
return TRY(data_buffer.write_buffered<1024>(buffer_size, [&](Bytes bytes) {
|
2021-09-01 09:44:55 +03:00
|
|
|
get_good_random_bytes(bytes);
|
|
|
|
return bytes.size();
|
2021-09-06 03:30:27 +03:00
|
|
|
}));
|
2020-07-31 00:38:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|