blob: ad17e3b40874f13bb14b8136592dd8214de64e0d [file]
// SPDX-License-Identifier: GPL-2.0-only
// Copyright (c) 2026 Jarkko Sakkinen
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "base.h"
#include "ids.h"
#include "uapi.h"
#define DEFAULT_MAX_BLOCKS_PER_USER 64
#define KEY_USER_BYTES ((ID_TOTAL_BLOCKS + CHAR_BIT - 1) / CHAR_BIT)
static const char whitespace[] = " \t\n\v\f\r";
enum id_block_state {
BLOCK_FREE,
BLOCK_ACTIVE,
BLOCK_QUARANTINED,
BLOCK_RETIRED,
};
struct id_block_slot {
enum id_block_state state;
uint32_t owner_uid;
};
static void record_key_user(char *line, unsigned char *blocks)
{
char *colon, *start, *end;
unsigned long uid;
size_t idx;
colon = strchr(line, ':');
if (!colon)
return;
*colon = '\0';
start = line + strspn(line, whitespace);
if (*start < '0' || *start > '9')
return;
errno = 0;
uid = strtoul(start, &end, 10);
if (errno || end + strspn(end, whitespace) != colon ||
uid < ID_WINDOW_FIRST || uid > ID_WINDOW_LAST)
return;
idx = (uid - ID_WINDOW_FIRST) / ID_BLOCK_SIZE;
blocks[idx / CHAR_BIT] |= 1U << (idx % CHAR_BIT);
}
static int read_key_users(unsigned char *blocks)
{
char *line = NULL;
size_t cap = 0;
FILE *file;
int ret = 0;
file = fopen("/proc/key-users", "re");
if (!file) {
/* Kernels without key support do not expose this file. */
if (errno == ENOENT)
return 0;
return err_ctx(-errno, "open /proc/key-users");
}
for (;;) {
errno = 0;
if (getline(&line, &cap, file) < 0) {
if (!feof(file))
ret = err_ctx(errno ? -errno : -EIO,
"read /proc/key-users");
break;
}
record_key_user(line, blocks);
}
free(line);
fclose(file);
return ret;
}
static bool block_has_key_user(const unsigned char *blocks, size_t idx)
{
return blocks[idx / CHAR_BIT] & (1U << (idx % CHAR_BIT));
}
static void reclaim_quarantined(struct id_block_pool *pool,
const unsigned char *blocks)
{
struct id_block_slot *slot;
size_t idx;
for (idx = 0; idx < ID_TOTAL_BLOCKS; idx++) {
slot = &pool->slots[idx];
if (slot->state == BLOCK_QUARANTINED &&
!block_has_key_user(blocks, idx))
*slot = (struct id_block_slot){0};
}
}
int id_block_pool_init(struct id_block_pool *pool)
{
struct id_block_slot *slots;
slots = calloc(ID_TOTAL_BLOCKS, sizeof(*slots));
if (!slots)
return err_ctx(-ENOMEM, "allocate cell ID block pool");
*pool = (struct id_block_pool){
.max_per_user = DEFAULT_MAX_BLOCKS_PER_USER,
.slots = slots,
};
return 0;
}
void id_block_pool_destroy(struct id_block_pool *pool)
{
free(pool->slots);
*pool = (struct id_block_pool){0};
}
int id_block_allocate(struct id_block_pool *pool, uint32_t owner_uid,
struct id_block_lease *out)
{
unsigned char blocks[KEY_USER_BYTES] = {0};
struct id_block_slot *slot;
size_t idx, offset, used = 0;
int ret;
ret = read_key_users(blocks);
if (ret)
return ret;
reclaim_quarantined(pool, blocks);
/* Retired blocks continue to count against their original owner. */
for (idx = 0; idx < ID_TOTAL_BLOCKS; idx++) {
slot = &pool->slots[idx];
if (slot->state != BLOCK_FREE && slot->owner_uid == owner_uid)
used++;
}
if (used >= pool->max_per_user)
return err_ctx(-ENOSPC, "per-user cell ID block limit reached");
for (offset = 0; offset < ID_TOTAL_BLOCKS; offset++) {
idx = (pool->next_cursor + offset) % ID_TOTAL_BLOCKS;
slot = &pool->slots[idx];
if (slot->state != BLOCK_FREE ||
block_has_key_user(blocks, idx))
continue;
*slot = (struct id_block_slot){BLOCK_ACTIVE, owner_uid};
pool->next_cursor = (idx + 1) % ID_TOTAL_BLOCKS;
*out = (struct id_block_lease){
.first_id = ID_WINDOW_FIRST + idx * ID_BLOCK_SIZE,
.owner_uid = owner_uid,
};
return 0;
}
return err_ctx(-ENOSPC, "no free 65536-ID cell blocks available");
}
void id_block_release(struct id_block_pool *pool,
const struct id_block_lease *lease, bool retire)
{
unsigned char blocks[KEY_USER_BYTES] = {0};
struct id_block_slot *slot;
uint32_t offset;
if (lease->first_id < ID_WINDOW_FIRST ||
lease->first_id > ID_WINDOW_LAST)
return;
offset = lease->first_id - ID_WINDOW_FIRST;
if (offset % ID_BLOCK_SIZE)
return;
slot = &pool->slots[offset / ID_BLOCK_SIZE];
if (slot->state != BLOCK_ACTIVE || slot->owner_uid != lease->owner_uid)
return;
if (retire) {
slot->state = BLOCK_RETIRED;
return;
}
slot->state = BLOCK_QUARANTINED;
if (!read_key_users(blocks))
reclaim_quarantined(pool, blocks);
}