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