linux-sysprog · intermediate · ~15 min
Robust field extraction from /proc text.
Find the VmRSS: line in a /proc/self/status blob and return its kilobyte value — the resident memory size of a process.
Implement long extract_vm_rss_kb(const char *status_blob) that returns the VmRSS value in kB.
status_blob: the full text of a /proc/<pid>/status file, with lines like:Name: cat
Pid: 12345
VmRSS: 1240 kB
Threads: 1
Returns the integer that follows VmRSS: (in kB), or -1 if the field is absent, the input is NULL, or the value cannot be parsed.
extract_vm_rss_kb(blob containing "VmRSS:\t 1240 kB") -> 1240
extract_vm_rss_kb("VmRSS:\t 0 kB\n") -> 0
extract_vm_rss_kb("Name:\tx\nPid:\t1\n") -> -1 (no VmRSS line)
extract_vm_rss_kb(NULL) -> -1
NULL: -1.In the Kali toolchain, the first thing you do on a foothold box is enumerate its own posture: who am I (uid/gid), what can I do (capabilities), what's loaded into me. /proc/self/status exposes all of that in a flat text format that an attacker would parse the same way a defender does — to know whether the process is privileged. We're writing the defender's parser: read the file's bytes, pull out the fields, hand them up the stack. No privilege escalation, no kernel writes — just a structured read of a virtual file every Linux process gets for free.
The full /proc/
The integer after 'VmRSS:' in kB, or -1 if absent/NULL/unparseable.
Match the VmRSS field and read the number after it; a value of 0 is valid.
long extract_vm_rss_kb(const char *status_blob) { /* TODO */ (void)status_blob; return -1; }
Matching VmRSS inside another field like VmRSSData. Pin to start-of-line.
Field absent. Field with leading spaces.
O(strlen).
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.