Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -254,3 +254,5 @@ fat32cap.log
fat32cap-trace.log
idelba.log
idelba-trace.log
uidcaps.log
uidcaps-trace.log
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ SRC := \
src/fdprobe_elf_data.c \
src/tcpcap_elf_data.c \
src/fatprobe_elf_data.c \
src/capprobe_elf_data.c \
src/slotbomb_elf_data.c \
src/slothold_elf_data.c \
src/shell_elf_data.c \
Expand Down
3 changes: 3 additions & 0 deletions doc/SECURITY_ARCHITECTURAL_LIMITATIONS.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ There is no general rlimit mechanism and **no per-process memory or CPU-time quo
- **Task-creation rate:** token bucket (burst 10, 5/s sustained), `-EAGAIN` when empty (`src/process.c`).
- **ramfs file descriptors:** per-process `PROCESS_MAX_FDS` (`-EMFILE`), plus a per-uid cap (`RAMFS_USER_MAX_FDS`, 8) and a root reserve (`RAMFS_ROOT_RESERVED_FDS`, 4) — `src/ramfs.c`, `src/ramfs.h`.
- **TCP sockets:** per-uid cap (`TCP_USER_MAX_SOCKETS`, 2) and a root reserve (`TCP_ROOT_RESERVED_SOCKETS`, 2) — `src/tcp.c`, `src/tcp.h`.
- **FAT32 open files:** per-uid cap (`FAT32_USER_MAX_FDS`, 8) and a root reserve (`FAT32_ROOT_RESERVED_FDS`, 4) — `src/fat32.c`.
- **Pipes:** per-uid cap (`PIPE_USER_MAX`, 4) and a root reserve (`PIPE_ROOT_RESERVED`, 2) on the 8-slot table, `-EAGAIN` — `src/syscall.c`.
- **VFS descriptors:** per-process `TASK_FDTABLE_SIZE` (16, `-EMFILE`), plus a per-uid cap (`VFS_USER_MAX_FDS`, 24, `-EAGAIN`) and a root reserve (`VFS_RESERVED_FDS`, 6) on the 64-slot table — `src/vfs.c`, `src/vfs.h`. This is the only cap a ramfs directory open meets.

**Remaining gap:** physical memory and CPU time are not accounted per process or per user.

Expand Down
13 changes: 3 additions & 10 deletions doc/SECURITY_AUDIT_2026-10.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# TinyOS Security Audit — 2026-10 (TCP, memory, ramfs/VFS, credentials, pipes)

> **STATUS: every confirmed finding is FIXED AND MERGED (PRs #183–#188, `main` @
> `e6012cc`).** §3 lists what was examined and deliberately *not* changed — the
> `e6012cc`). The two per-uid caps §3 used to list as gaps are added by #191.** §3 lists what was examined and deliberately *not* changed — the
> latent hazards, accepted trade-offs and gaps that are not worth re-reporting
> without new information.

Expand Down Expand Up @@ -48,6 +48,8 @@ login prompt. Harness: `verify-logout-session-teardown.sh`.
| #187 | Creds | The spray detector compared a truncated stored name against the full one with `strcmp`, so one over-long name counted as many | same |
| #188 | Memory | `task_create_user_argv()`'s argv-does-not-fit branch freed only the slot (24 frames leaked, measured) | `verify-memory-fixes.sh` |
| #188 | Memory | `pae_create_user_pdpt()` panicked on allocation failure, though it runs on every exec/spawn | same |
| #191 | Pipes | No per-uid share of the 8-slot pipe table: one user holding all 8 failed every other user's `PIPE_OP_CREATE`. Now 4 per uid plus a 2-slot root reserve, `-EAGAIN` | `verify-uid-caps.sh` |
| #191 | VFS | Directory opens met no per-uid cap (a ramfs directory open takes no ramfs fd), so one user could fill the VFS descriptor pool. Now 24 per uid, `-EAGAIN` | same |

#187 also replaced `/etc/issue`'s `root/root` and `user/user` hints with
neutral text. The text lives in `user_create_etc_structure()`, which nothing
Expand Down Expand Up @@ -77,15 +79,6 @@ These are recorded so they are not re-reported as new findings.
- **Any user can keep root locked out.** The lockout counter is per target
account, so three failed `su root` a minute keep root locked. This is an
accepted trade-off; see "Account lockout" in `doc/SECURITY_HARDENING.md`.
- **No per-uid pipe cap.** `MAX_PIPES` is 8 system-wide (`src/syscall.c`). One
user holding all 8 makes every other user's `PIPE_OP_CREATE` return `-EMFILE`.
The pipes are released when the holder exits, so this is a DoS only while that
user's processes live. If it matters, the fix is the pattern from FAT32 PR
#181: a per-uid share plus a root reserve.
- **No per-uid cap on directory opens.** Since #186 an exhausted pool returns
`-ENFILE` instead of panicking. But directory opens are not charged to the
per-uid fd cap, so one ordinary user can still fill most of the shared VFS
descriptor table and starve other users' opens. The fix is the same pattern.
- **Kernel-shell `exec /shell.elf | cat` does not hang; the probe lost its
keystrokes.** Kernel-shell pipelines run their stages one after another (see
"Pipeline execution" in `src/shell.c`). An interactive program as stage 1
Expand Down
1,597 changes: 1,597 additions & 0 deletions src/capprobe_elf_data.c

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions src/capprobe_elf_data.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#pragma once

/* Embedded capprobe.elf.signed ELF executable (ECDSA signed) */
extern const unsigned char capprobe_elf_data[];
extern const unsigned int capprobe_elf_data_len;
12 changes: 12 additions & 0 deletions src/kernel.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
#include "fdprobe_elf_data.h"
#include "tcpcap_elf_data.h"
#include "fatprobe_elf_data.h"
#include "capprobe_elf_data.h"
#include "slotbomb_elf_data.h"
#include "slothold_elf_data.h"
#include "shell_elf_data.h"
Expand Down Expand Up @@ -1087,6 +1088,17 @@ void kernel_main(uint32_t magic, uint32_t info_ptr) {
}
}

/* capprobe.elf fills the pipe table and the VFS descriptor table as one
* unprivileged uid. No builtin can hold either. See verify-uid-caps.sh. */
{
int capprobe_fd = ramfs_open("/capprobe.elf", RAMFS_FLAG_WRITE);
if (capprobe_fd >= 0) {
ramfs_write(capprobe_fd, capprobe_elf_data, capprobe_elf_data_len);
ramfs_close(capprobe_fd);
ramfs_chmod("/capprobe.elf", 0755);
}
}

/* slotbomb.elf spawns /slothold.elf in a tight loop WITHOUT reaping, to
* prove the per-user concurrent task cap engages (see verify-slotcap.sh).
* Like credprobe.elf it has to bypass the shell: the cap lives in
Expand Down
66 changes: 52 additions & 14 deletions src/syscall.c
Original file line number Diff line number Diff line change
Expand Up @@ -2545,10 +2545,21 @@ int sys_redirect(int fd, const char* user_path, int mode) {
*===========================================================================*/
#define MAX_PIPES 8

/* The table is shared by every user, so without a share one uid could hold all
* 8 and every other user's PIPE_OP_CREATE would fail until that uid's processes
* exited. A non-root uid may hold PIPE_USER_MAX, and the last
* PIPE_ROOT_RESERVED free slots are root's -- the ramfs and FAT32 open-table
* pattern. A ring-3 pipeline holds one pipe, so this is four concurrent
* pipelines per user. */
#define PIPE_USER_MAX 4
#define PIPE_ROOT_RESERVED 2

typedef struct {
pipe_buffer_t* buf; /* NULL when the slot is free */
uint32_t owner_pid;
uint32_t owner_generation;
uint32_t owner_uid; /* charged until the buffer is freed, so an
* orphan a child still holds counts too */
uint32_t pages; /* frames backing buf, for the free path */
bool orphaned; /* released, waiting for its last reader/writer */
} pipe_slot_t;
Expand Down Expand Up @@ -2667,6 +2678,7 @@ static void pipe_slot_reap(pipe_slot_t* slot) {
}
slot->buf = NULL;
slot->pages = 0;
slot->owner_uid = 0;
slot->orphaned = false;
}

Expand Down Expand Up @@ -2733,17 +2745,11 @@ int sys_pipe(int op, int id) {

if (op == PIPE_OP_CREATE) {
pipe_reap_orphans();
int slot_idx = -1;
for (int i = 0; i < MAX_PIPES; i++) {
if (!pipe_table[i].buf) {
slot_idx = i;
break;
}
}
if (slot_idx < 0) {
return -EMFILE;
}

/* Build the buffer first and claim a slot afterwards, inside one
* critical section: the count, the choice of slot and the claim must
* not be split by a switch to another creator. A refused create frees
* what it built. */
uint32_t phys = pmm_alloc_contiguous((uint32_t)PIPE_FRAMES);
if (phys == 0) {
return -ENOMEM;
Expand Down Expand Up @@ -2775,10 +2781,42 @@ int sys_pipe(int op, int id) {
return -ENOMEM;
}

pipe_table[slot_idx].buf = buf;
pipe_table[slot_idx].owner_pid = self->pid;
pipe_table[slot_idx].owner_generation = self->generation;
pipe_table[slot_idx].pages = (uint32_t)PIPE_FRAMES;
int slot_idx = -1, avail = 0, uid_held = 0;
CRITICAL_SECTION_ENTER();
for (int i = 0; i < MAX_PIPES; i++) {
if (!pipe_table[i].buf) {
avail++;
if (slot_idx < 0) {
slot_idx = i;
}
} else if (pipe_table[i].owner_uid == self->uid) {
uid_held++;
}
}
int refuse = 0;
if (slot_idx < 0) {
refuse = -EMFILE;
} else if (self->euid != 0 &&
(uid_held >= PIPE_USER_MAX || avail <= PIPE_ROOT_RESERVED)) {
refuse = -EAGAIN;
kdbg("[PIPE] create refused: uid %u holds %d, %d slots free\n",
(unsigned)self->uid, uid_held, avail);
} else {
pipe_table[slot_idx].buf = buf;
pipe_table[slot_idx].owner_pid = self->pid;
pipe_table[slot_idx].owner_generation = self->generation;
pipe_table[slot_idx].owner_uid = self->uid;
pipe_table[slot_idx].pages = (uint32_t)PIPE_FRAMES;
}
CRITICAL_SECTION_EXIT();

if (refuse) {
pipe_destroy(buf);
for (uint32_t i = 0; i < (uint32_t)PIPE_FRAMES; i++) {
pmm_free(phys + i * 4096);
}
return refuse;
}

pipe_bind_stream(&self->streams.stdout_stream, buf);
return slot_idx + 1;
Expand Down
5 changes: 3 additions & 2 deletions src/syscall.h
Original file line number Diff line number Diff line change
Expand Up @@ -974,8 +974,9 @@ int sys_redirect(int fd, const char* user_path, int mode);
* @param op One of the PIPE_OP_* values above
* @param id Pipe ID from a previous CREATE; ignored by CREATE itself
* @return CREATE returns a positive pipe ID; the others return 0 on success.
* -EMFILE if no pipe slot is free, -ENOMEM if the buffer could not be
* allocated, -EBADF for an unknown ID, -EPERM for another process's
* -EMFILE if no pipe slot is free, -EAGAIN if a non-root caller's uid
* already holds PIPE_USER_MAX pipes or only root's reserve is left,
* -ENOMEM if the buffer could not be allocated, -EBADF for an unknown ID, -EPERM for another process's
* pipe, -EINVAL for an unknown op.
*/
int sys_pipe(int op, int id);
Expand Down
33 changes: 30 additions & 3 deletions src/vfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -281,9 +281,16 @@ void vfs_foreach_mount(void (*callback)(char drive_letter, const char* driver_na
* falls back to the reserved pool (last VFS_RESERVED_FDS entries).
*
* If the reserved pool is exhausted too, the open fails with VFS_ENFILE.
* It used to panic: root's ring-3 processes can hold every slot (directory
* opens count against no per-uid cap, and a process may hold 16), so a few
* of them opening D:/ until refused took the whole system down.
* It used to panic: root's ring-3 processes can hold every slot (a process
* may hold 16), so a few of them opening D:/ until refused took the whole
* system down.
*
* Each unprivileged uid may hold VFS_USER_MAX_FDS slots, and is refused with
* VFS_EAGAIN past that. This is the only cap a directory open meets: ramfs
* directory opens take no ramfs fd, so the ramfs per-uid share never saw
* them, and one user could fill the primary pool and starve every other
* user's opens. The count comes from the slots' owner fields (vfs_free_fd
* zeroes the slot), so it cannot drift.
*---------------------------------------------------------------------------*/
static int vfs_alloc_fd(void) {
CRITICAL_SECTION_ENTER();
Expand All @@ -296,12 +303,31 @@ static int vfs_alloc_fd(void) {
is_privileged = (current->uid == 0) || (current->capabilities & CAP_SYSTEM_CRITICAL);
}

uint32_t uid = current ? current->uid : 0;

if (current != NULL && !is_privileged) {
int uid_held = 0;
for (int i = 0; i < VFS_MAX_FDS; i++) {
if (vfs_fd_table[i].in_use && vfs_fd_table[i].owner_uid == uid) {
uid_held++;
}
}
if (uid_held >= VFS_USER_MAX_FDS) {
CRITICAL_SECTION_EXIT();
/* Ring 3 reaches this: a trace, not a console line */
kdbg("[VFS] open refused: uid %u holds %d descriptors\n",
(unsigned)uid, uid_held);
return VFS_EAGAIN;
}
}

/* Primary pool: available to all processes */
int primary_limit = VFS_MAX_FDS - VFS_RESERVED_FDS;

for (int i = 0; i < primary_limit; i++) {
if (!vfs_fd_table[i].in_use) {
vfs_fd_table[i].in_use = true;
vfs_fd_table[i].owner_uid = uid;
CRITICAL_SECTION_EXIT();
return i;
}
Expand All @@ -317,6 +343,7 @@ static int vfs_alloc_fd(void) {
for (int i = primary_limit; i < VFS_MAX_FDS; i++) {
if (!vfs_fd_table[i].in_use) {
vfs_fd_table[i].in_use = true;
vfs_fd_table[i].owner_uid = uid;
CRITICAL_SECTION_EXIT();
return i; /* Not logged: root's opens reach it at will */
}
Expand Down
4 changes: 4 additions & 0 deletions src/vfs.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ typedef int32_t ssize_t; /* Signed size type for I/O operations *
*=============================================================================*/
#define VFS_MAX_FDS 64 /* Maximum file descriptors */
#define VFS_RESERVED_FDS 6 /* Reserved FDs for critical processes (10% of VFS_MAX_FDS) */
#define VFS_USER_MAX_FDS 24 /* Per-uid share for unprivileged callers: above one
* process's TASK_FDTABLE_SIZE (16), so a process
* with a full table can still exec and spawn */
#define VFS_MAX_PATH 256 /* Maximum path length */

/* Drive used for a path with no "X:" prefix. RAMFS holds the embedded system
Expand Down Expand Up @@ -406,6 +409,7 @@ typedef struct vfs_file_descriptor {
size_t offset; /* Current offset (for seek) */
void* private_data; /* Driver-specific data */
const file_operations_t* ops; /* Operations for this FD */
uint32_t owner_uid; /* Real uid charged for this slot */
char path[VFS_MAX_PATH]; /* Path (for debugging) */
} vfs_file_descriptor_t;

Expand Down
9 changes: 8 additions & 1 deletion userspace/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ PROGRAMS := hello.elf shell.elf info.elf sleeper.elf spawner.elf fileio.elf \
producer.elf counter.elf credprobe.elf slotbomb.elf slothold.elf \
netprobe.elf msealprobe.elf callprobe.elf \
busyprobe.elf pipeprobe.elf fdprobe.elf tcpcap.elf \
setuidprobe.elf fatprobe.elf
setuidprobe.elf fatprobe.elf capprobe.elf

# Common runtime objects for libc-based programs
LIBC_OBJS := crt0.o libc.o
Expand Down Expand Up @@ -188,6 +188,13 @@ fatprobe.elf: fatprobe.c libc.h $(LIBC_OBJS) user.ld
$(LD) $(LDFLAGS) -o fatprobe.elf crt0.o fatprobe.o libc.o -lgcc
@echo "Built fatprobe.elf"

# capprobe.elf fills the pipe table and the VFS descriptor table as one uid,
# from a parent and a spawned child. See verify-uid-caps.sh.
capprobe.elf: capprobe.c libc.h $(LIBC_OBJS) user.ld
$(CC) $(CFLAGS) -c capprobe.c -o capprobe.o
$(LD) $(LDFLAGS) -o capprobe.elf crt0.o capprobe.o libc.o -lgcc
@echo "Built capprobe.elf"

# shell.elf, like the rest, gets _start from crt0 and main(argc, argv)
shell.elf: shell.c libc.h $(LIBC_OBJS) user.ld
$(CC) $(CFLAGS) -c shell.c -o shell.o
Expand Down
Loading
Loading