#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <stdbool.h>
// --- Configuration & Constants ---
#define MAX_PROCESSES 4
#define MAX_CAPS_PER_PROCESS 8
#define MSG_BUFFER_SIZE 256
// --- Error Codes ---
typedef enum {
IPC_OK = 0,
IPC_ERR_INVALID_CAP = -1, // Sender doesn't hold the capability
IPC_ERR_TARGET_DOWN = -2, // Target process doesn't exist
IPC_ERR_PERMISSION = -3, // Capability doesn't allow this operation
IPC_ERR_BUFFER_OVERRUN = -4
} ipc_status_t;
// --- Security: Capability Definition ---
// A capability represents a 'key' to access a resource.
typedef struct {
int target_endpoint_id; // The object we are talking to
uint32_t permissions; // Bitmask (READ, WRITE, GRANT)
bool valid; // Is this slot used?
} capability_t;
// bit flags for permissions
#define PERM_READ (1 << 0)
#define PERM_WRITE (1 << 1)
#define PERM_GRANT (1 << 2) // Ability to share this cap with others
// --- Kernel Objects ---
// Message structure passed via IPC
typedef struct {
uint8_t data[MSG_BUFFER_SIZE];
size_t len;
// In a real kernel, we might also pass capabilities here (Rights Ambplification)
} ipc_message_t;
// Process Control Block (PCB)
typedef struct {
int pid;
char name[16];
capability_t caps[MAX_CAPS_PER_PROCESS]; // The C-Space (Capability Space)
// IPC State
bool blocked; // Is the process waiting for a message?
int blocking_on_endpoint; // Which endpoint is it waiting on?
ipc_message_t msg_buffer; // Kernel buffer for the message
} pcb_t;
// Global Kernel State
pcb_t process_table[MAX_PROCESSES];
int next_free_pid = 0;
// --- Helper: Simulate Process Creation ---
int create_process(const char* name) {
if (next_free_pid >= MAX_PROCESSES) return -1;
pcb_t *p = &process_table[next_free_pid];
p->pid = next_free_pid;
strncpy(p->name, name, 15);
p->blocked = false;
p->blocking_on_endpoint = -1;
// Initialize capabilities as empty
for(int i=0; i<MAX_CAPS_PER_PROCESS; i++) {
p->caps[i].valid = false;
}
return next_free_pid++;
}
// --- Helper: Grant a Capability (System Call Simulation) ---
// In a real system, this would be done by the initial loader or a parent process
bool kernel_grant_capability(int holder_pid, int slot, int target_endpoint, uint32_t perms) {
if (holder_pid >= MAX_PROCESSES || slot >= MAX_CAPS_PER_PROCESS) return false;
pcb_t *p = &process_table[holder_pid];
p->caps[slot].target_endpoint_id = target_endpoint;
p->caps[slot].permissions = perms;
p->caps[slot].valid = true;
return true;
}
// --- The Secure IPC Mechanism ---
/**
* SEND:
* 1. Validate the sender has the capability to the endpoint.
* 2. Find the receiver (mapped by endpoint ID for this simple simulation).
* 3. Copy data to kernel buffer (Isolation).
* 4. Block sender until receiver replies.
*/
ipc_status_t sys_ipc_send(int sender_pid, int cap_slot, const void* data, size_t len) {
pcb_t *sender = &process_table[sender_pid];
// 1. Security Check: Does the sender have this capability?
if (cap_slot >= MAX_CAPS_PER_PROCESS || !sender->caps[cap_slot].valid) {
return IPC_ERR_INVALID_CAP;
}
capability_t *cap = &sender->caps[cap_slot];
// 2. Permission Check: Do we have write rights to this endpoint?
if (!(cap->permissions & PERM_WRITE)) {
return IPC_ERR_PERMISSION;
}
// Map endpoint ID to Receiver PID (Simplified: Endpoint ID == PID for this demo)
int receiver_pid = cap->target_endpoint_id;
if (receiver_pid >= MAX_PROCESSES) return IPC_ERR_TARGET_DOWN;
pcb_t *receiver = &process_table[receiver_pid];
// 3. Data Size Check
if (len > MSG_BUFFER_SIZE) return IPC_ERR_BUFFER_OVERRUN;
// 4. Synchronization Check
// We implement a simple handshake. If receiver isn't listening yet, we block.
// (In a real microkernel, this would be a queue or a thread state change).
if (!receiver->blocked || receiver->blocking_on_endpoint != sender->pid) {
// In a real async system, we would block here.
// For this simulation, we return an error or wait.
printf("[Kernel] Process %s blocked waiting for %s...\n", sender->name, receiver->name);
sender->blocked = true;
// ... scheduling logic would context switch here ...
}
// 5. The Secure Copy
// This is the heart of secure IPC. Data is copied FROM user memory
// TO kernel memory, then TO destination user memory.
printf("[Kernel] Securely copying %zu bytes from %s to %s\n", len, sender->name, receiver->name);
// Simulating the copy into receiver's buffer
memcpy(receiver->msg_buffer.data, data, len);
receiver->msg_buffer.len = len;
// Unblock receiver
receiver->blocked = false;
return IPC_OK;
}
/**
* RECV:
* 1. Block the process on a specific endpoint.
* 2. Wait for a sender who holds a capability to this endpoint.
*/
ipc_status_t sys_ipc_recv(int receiver_pid, void* buffer, size_t max_len) {
pcb_t *receiver = &process_table[receiver_pid];
// Block self
receiver->blocked = true;
receiver->blocking_on_endpoint = -1; // Wait for anyone (or specific in complex systems)
printf("[Kernel] Process %s listening for IPC...\n", receiver->name);
// ... In a real kernel, we would context switch OUT here ...
// ... When we wake up, msg_buffer contains the data ...
// Simulate receiving data immediately for the demo logic
if (receiver->msg_buffer.len > 0) {
if (receiver->msg_buffer.len > max_len) return IPC_ERR_BUFFER_OVERRUN;
memcpy(buffer, receiver->msg_buffer.data, receiver->msg_buffer.len);
return IPC_OK;
}
return IPC_OK; // Would normally block
}
// --- Main: Demonstration ---
int main() {
// 1. Setup: Create a Client and a Server (Driver)
int pid_server = create_process("FileServer");
int pid_client = create_process("TextEditor");
// 2. Capability Distribution (The Security Policy)
// We give Client a capability in slot 0 that points to Server.
// It has WRITE permission (can send requests).
kernel_grant_capability(pid_client, 0, pid_server, PERM_READ | PERM_WRITE);
// 3. The Client attempts IPC
const char *request = "OPEN: secret.txt";
printf("\n--- Client Attempting IPC ---\n");
ipc_status_t status = sys_ipc_send(pid_client, 0, request, strlen(request) + 1);
if (status == IPC_OK) {
printf("IPC Success: Message sent securely.\n");
} else {
printf("IPC Failed: Error code %d\n", status);
}
// 4. Demonstrate Failure (Security Violation)
printf("\n--- Client Attempting Unauthorized IPC ---\n");
// Client tries to use slot 1, which was never granted.
// Or client tries to use a slot with no WRITE perms.
// Let's create a "LogService" but only give READ perms (notification only)
int pid_logger = create_process("Logger");
kernel_grant_capability(pid_client, 1, pid_logger, PERM_READ);
// Client tries to WRITE to Logger
status = sys_ipc_send(pid_client, 1, "Log this", 8);
if (status != IPC_OK) {
printf("IPC Blocked by Kernel: Permission Denied (Expected Behavior).\n");
}
return 0;
}