2020-07-30 23:38:15 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-07-30 23:38:15 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <Kernel/Random.h>
|
2023-02-24 19:45:37 +02:00
|
|
|
#include <Kernel/Tasks/Process.h>
|
2020-09-11 21:11:07 -06:00
|
|
|
#include <Kernel/UserOrKernelBuffer.h>
|
2020-07-30 23:38:15 +02: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 00:51:39 +01:00
|
|
|
ErrorOr<FlatPtr> Process::sys$getrandom(Userspace<void*> buffer, size_t buffer_size, [[maybe_unused]] unsigned flags)
|
2020-07-30 23:38:15 +02:00
|
|
|
{
|
2021-08-06 14:43:02 +03:00
|
|
|
VERIFY_NO_PROCESS_BIG_LOCK(this);
|
2021-12-29 01:11:45 -08:00
|
|
|
TRY(require_promise(Pledge::stdio));
|
2021-06-16 16:44:15 +02:00
|
|
|
if (buffer_size > NumericLimits<ssize_t>::max())
|
2021-03-01 13:49:16 +01:00
|
|
|
return EINVAL;
|
2020-07-30 23:38:15 +02:00
|
|
|
|
2021-11-21 12:24:32 +01:00
|
|
|
auto data_buffer = TRY(UserOrKernelBuffer::for_user_buffer(buffer, buffer_size));
|
2021-09-06 02:30:27 +02:00
|
|
|
|
2021-11-21 12:24:32 +01:00
|
|
|
return TRY(data_buffer.write_buffered<1024>(buffer_size, [&](Bytes bytes) {
|
2021-08-31 23:44:55 -07:00
|
|
|
get_good_random_bytes(bytes);
|
|
|
|
return bytes.size();
|
2021-09-06 02:30:27 +02:00
|
|
|
}));
|
2020-07-30 23:38:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|