lib/vdso/getrandom.c
Source file repositories/reference/linux-study-clean/lib/vdso/getrandom.c
File Facts
- System
- Linux kernel
- Corpus path
lib/vdso/getrandom.c- Extension
.c- Size
- 9959 bytes
- Lines
- 268
- Domain
- Kernel Services
- Bucket
- lib
- Inferred role
- Kernel Services: implementation source
- Status
- source implementation candidate
Why This File Exists
Shared kernel service surface used by multiple subsystems, including helpers, cryptography, virtualization support, and async I/O infrastructure.
- Shared kernel service surface used by multiple subsystems, including helpers, cryptography, virtualization support, and async I/O infrastructure.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
linux/array_size.hlinux/minmax.hvdso/datapage.hvdso/getrandom.hvdso/limits.hvdso/unaligned.hasm/barrier.hasm/vdso/getrandom.huapi/linux/errno.huapi/linux/mman.huapi/linux/random.hvdso/vsyscall.h
Detected Declarations
function Copyrightfunction memcpy_and_zero_srcfunction __cvdso_getrandom_datafunction __cvdso_getrandom
Annotated Snippet
if (getrandom_syscall(state->key, sizeof(state->key), 0) != sizeof(state->key)) {
/*
* If the syscall failed to refresh the key, then @state->key is now
* invalid, so invalidate the generation so that it is not used again, and
* fallback to using the syscall entirely.
*/
WRITE_ONCE(state->generation, 0);
/*
* Set @state->in_use to false only after the last write to @state in the
* line above.
*/
WRITE_ONCE(state->in_use, false);
goto fallback_syscall;
}
/*
* Set @state->pos to beyond the end of the batch, so that the batch is refilled
* using the new key.
*/
state->pos = sizeof(state->batch);
}
/* Set len to the total amount of bytes that this function is allowed to read, ret. */
len = ret;
more_batch:
/*
* First use bytes out of @state->batch, which may have been filled by the last call to this
* function.
*/
batch_len = min_t(size_t, sizeof(state->batch) - state->pos, len);
if (batch_len) {
/* Zeroing at the same time as memcpying helps preserve forward secrecy. */
memcpy_and_zero_src(buffer, state->batch + state->pos, batch_len);
state->pos += batch_len;
buffer += batch_len;
len -= batch_len;
}
if (!len) {
/* Prevent the loop from being reordered wrt ->generation. */
barrier();
/*
* Since @rng_info->generation will never be 0, re-read @state->generation, rather
* than using the local current_generation variable, to learn whether a fork
* occurred or if @state was zeroed due to memory pressure. Primarily, though, this
* indicates whether the kernel's RNG has reseeded, in which case generate a new key
* and start over.
*/
if (unlikely(READ_ONCE(state->generation) != READ_ONCE(rng_info->generation))) {
/*
* Prevent this from looping forever in case of low memory or racing with a
* user force-reseeding the kernel's RNG using the ioctl.
*/
if (have_retried) {
WRITE_ONCE(state->in_use, false);
goto fallback_syscall;
}
have_retried = true;
buffer = orig_buffer;
goto retry_generation;
}
/*
* Set @state->in_use to false only when there will be no more reads or writes of
* @state.
*/
WRITE_ONCE(state->in_use, false);
return ret;
}
/* Generate blocks of RNG output directly into @buffer while there's enough room left. */
nblocks = len / CHACHA_BLOCK_SIZE;
if (nblocks) {
__arch_chacha20_blocks_nostack(buffer, state->key, counter, nblocks);
buffer += nblocks * CHACHA_BLOCK_SIZE;
len -= nblocks * CHACHA_BLOCK_SIZE;
}
BUILD_BUG_ON(sizeof(state->batch_key) % CHACHA_BLOCK_SIZE != 0);
/* Refill the batch and overwrite the key, in order to preserve forward secrecy. */
__arch_chacha20_blocks_nostack(state->batch_key, state->key, counter,
sizeof(state->batch_key) / CHACHA_BLOCK_SIZE);
/* Since the batch was just refilled, set the position back to 0 to indicate a full batch. */
state->pos = 0;
Annotation
- Immediate include surface: `linux/array_size.h`, `linux/minmax.h`, `vdso/datapage.h`, `vdso/getrandom.h`, `vdso/limits.h`, `vdso/unaligned.h`, `asm/barrier.h`, `asm/vdso/getrandom.h`.
- Detected declarations: `function Copyright`, `function memcpy_and_zero_src`, `function __cvdso_getrandom_data`, `function __cvdso_getrandom`.
- Atlas domain: Kernel Services / lib.
- Implementation status: source implementation candidate.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.