Skip to content

Commit 3859a27

Browse files
committed
randstruct: Mark various structs for randomization
This marks many critical kernel structures for randomization. These are structures that have been targeted in the past in security exploits, or contain functions pointers, pointers to function pointer tables, lists, workqueues, ref-counters, credentials, permissions, or are otherwise sensitive. This initial list was extracted from Brad Spengler/PaX Team's code in the last public patch of grsecurity/PaX based on my understanding of the code. Changes or omissions from the original code are mine and don't reflect the original grsecurity/PaX code. Left out of this list is task_struct, which requires special handling and will be covered in a subsequent patch. Signed-off-by: Kees Cook <[email protected]>
1 parent ffa47aa commit 3859a27

38 files changed

+57
-56
lines changed

arch/x86/include/asm/processor.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ struct cpuinfo_x86 {
129129
/* Index into per_cpu list: */
130130
u16 cpu_index;
131131
u32 microcode;
132-
};
132+
} __randomize_layout;
133133

134134
struct cpuid_regs {
135135
u32 eax, ebx, ecx, edx;

fs/mount.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ struct mnt_namespace {
1616
u64 event;
1717
unsigned int mounts; /* # of mounts in the namespace */
1818
unsigned int pending_mounts;
19-
};
19+
} __randomize_layout;
2020

2121
struct mnt_pcp {
2222
int mnt_count;
@@ -68,7 +68,7 @@ struct mount {
6868
struct hlist_head mnt_pins;
6969
struct fs_pin mnt_umount;
7070
struct dentry *mnt_ex_mountpoint;
71-
};
71+
} __randomize_layout;
7272

7373
#define MNT_NS_INTERNAL ERR_PTR(-EINVAL) /* distinct from any mnt_namespace */
7474

fs/namei.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,7 @@ struct nameidata {
524524
struct inode *link_inode;
525525
unsigned root_seq;
526526
int dfd;
527-
};
527+
} __randomize_layout;
528528

529529
static void set_nameidata(struct nameidata *p, int dfd, struct filename *name)
530530
{

fs/proc/internal.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ struct proc_dir_entry {
5151
spinlock_t pde_unload_lock; /* proc_fops checks and pde_users bumps */
5252
u8 namelen;
5353
char name[];
54-
};
54+
} __randomize_layout;
5555

5656
union proc_op {
5757
int (*proc_get_link)(struct dentry *, struct path *);
@@ -70,7 +70,7 @@ struct proc_inode {
7070
struct list_head sysctl_inodes;
7171
const struct proc_ns_operations *ns_ops;
7272
struct inode vfs_inode;
73-
};
73+
} __randomize_layout;
7474

7575
/*
7676
* General functions
@@ -279,7 +279,7 @@ struct proc_maps_private {
279279
#ifdef CONFIG_NUMA
280280
struct mempolicy *task_mempolicy;
281281
#endif
282-
};
282+
} __randomize_layout;
283283

284284
struct mm_struct *proc_mem_open(struct inode *inode, unsigned int mode);
285285

include/linux/binfmts.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ struct linux_binprm {
4646
unsigned interp_flags;
4747
unsigned interp_data;
4848
unsigned long loader, exec;
49-
};
49+
} __randomize_layout;
5050

5151
#define BINPRM_FLAGS_ENFORCE_NONDUMP_BIT 0
5252
#define BINPRM_FLAGS_ENFORCE_NONDUMP (1 << BINPRM_FLAGS_ENFORCE_NONDUMP_BIT)
@@ -81,7 +81,7 @@ struct linux_binfmt {
8181
int (*load_shlib)(struct file *);
8282
int (*core_dump)(struct coredump_params *cprm);
8383
unsigned long min_coredump; /* minimal dump size */
84-
};
84+
} __randomize_layout;
8585

8686
extern void __register_binfmt(struct linux_binfmt *fmt, int insert);
8787

include/linux/cdev.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ struct cdev {
1717
struct list_head list;
1818
dev_t dev;
1919
unsigned int count;
20-
};
20+
} __randomize_layout;
2121

2222
void cdev_init(struct cdev *, const struct file_operations *);
2323

include/linux/cred.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ struct group_info {
3131
atomic_t usage;
3232
int ngroups;
3333
kgid_t gid[0];
34-
};
34+
} __randomize_layout;
3535

3636
/**
3737
* get_group_info - Get a reference to a group info structure
@@ -145,7 +145,7 @@ struct cred {
145145
struct user_namespace *user_ns; /* user_ns the caps and keyrings are relative to. */
146146
struct group_info *group_info; /* supplementary groups for euid/fsgid */
147147
struct rcu_head rcu; /* RCU deletion hook */
148-
};
148+
} __randomize_layout;
149149

150150
extern void __put_cred(struct cred *);
151151
extern void exit_creds(struct task_struct *);

include/linux/dcache.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ struct dentry {
113113
struct hlist_bl_node d_in_lookup_hash; /* only for in-lookup ones */
114114
struct rcu_head d_rcu;
115115
} d_u;
116-
};
116+
} __randomize_layout;
117117

118118
/*
119119
* dentry->d_lock spinlock nesting subclasses:

include/linux/fs.h

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ struct kiocb {
275275
void (*ki_complete)(struct kiocb *iocb, long ret, long ret2);
276276
void *private;
277277
int ki_flags;
278-
};
278+
} __randomize_layout;
279279

280280
static inline bool is_sync_kiocb(struct kiocb *kiocb)
281281
{
@@ -392,7 +392,7 @@ struct address_space {
392392
gfp_t gfp_mask; /* implicit gfp mask for allocations */
393393
struct list_head private_list; /* ditto */
394394
void *private_data; /* ditto */
395-
} __attribute__((aligned(sizeof(long))));
395+
} __attribute__((aligned(sizeof(long)))) __randomize_layout;
396396
/*
397397
* On most architectures that alignment is already the case; but
398398
* must be enforced here for CRIS, to let the least significant bit
@@ -435,7 +435,7 @@ struct block_device {
435435
int bd_fsfreeze_count;
436436
/* Mutex for freeze */
437437
struct mutex bd_fsfreeze_mutex;
438-
};
438+
} __randomize_layout;
439439

440440
/*
441441
* Radix-tree tags, for tagging dirty and writeback pages within the pagecache
@@ -653,7 +653,7 @@ struct inode {
653653
#endif
654654

655655
void *i_private; /* fs or device private pointer */
656-
};
656+
} __randomize_layout;
657657

658658
static inline unsigned int i_blocksize(const struct inode *node)
659659
{
@@ -868,7 +868,8 @@ struct file {
868868
struct list_head f_tfile_llink;
869869
#endif /* #ifdef CONFIG_EPOLL */
870870
struct address_space *f_mapping;
871-
} __attribute__((aligned(4))); /* lest something weird decides that 2 is OK */
871+
} __randomize_layout
872+
__attribute__((aligned(4))); /* lest something weird decides that 2 is OK */
872873

873874
struct file_handle {
874875
__u32 handle_bytes;
@@ -1005,7 +1006,7 @@ struct file_lock {
10051006
int state; /* state of grant or error if -ve */
10061007
} afs;
10071008
} fl_u;
1008-
};
1009+
} __randomize_layout;
10091010

10101011
struct file_lock_context {
10111012
spinlock_t flc_lock;
@@ -1404,7 +1405,7 @@ struct super_block {
14041405

14051406
spinlock_t s_inode_wblist_lock;
14061407
struct list_head s_inodes_wb; /* writeback inodes */
1407-
};
1408+
} __randomize_layout;
14081409

14091410
/* Helper functions so that in most cases filesystems will
14101411
* not need to deal directly with kuid_t and kgid_t and can
@@ -1690,7 +1691,7 @@ struct file_operations {
16901691
u64);
16911692
ssize_t (*dedupe_file_range)(struct file *, u64, u64, struct file *,
16921693
u64);
1693-
};
1694+
} __randomize_layout;
16941695

16951696
struct inode_operations {
16961697
struct dentry * (*lookup) (struct inode *,struct dentry *, unsigned int);

include/linux/fs_struct.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ struct fs_struct {
1212
int umask;
1313
int in_exec;
1414
struct path root, pwd;
15-
};
15+
} __randomize_layout;
1616

1717
extern struct kmem_cache *fs_cachep;
1818

include/linux/ipc.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@ struct kern_ipc_perm {
2020
umode_t mode;
2121
unsigned long seq;
2222
void *security;
23-
} ____cacheline_aligned_in_smp;
23+
} ____cacheline_aligned_in_smp __randomize_layout;
2424

2525
#endif /* _LINUX_IPC_H */

include/linux/ipc_namespace.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ struct ipc_namespace {
6161
struct ucounts *ucounts;
6262

6363
struct ns_common ns;
64-
};
64+
} __randomize_layout;
6565

6666
extern struct ipc_namespace init_ipc_ns;
6767
extern spinlock_t mq_lock;

include/linux/key-type.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ struct key_preparsed_payload {
4545
size_t datalen; /* Raw datalen */
4646
size_t quotalen; /* Quota length for proposed payload */
4747
time_t expiry; /* Expiry time of key */
48-
};
48+
} __randomize_layout;
4949

5050
typedef int (*request_key_actor_t)(struct key_construction *key,
5151
const char *op, void *aux);
@@ -158,7 +158,7 @@ struct key_type {
158158
/* internal fields */
159159
struct list_head link; /* link in types list */
160160
struct lock_class_key lock_class; /* key->sem lock class */
161-
};
161+
} __randomize_layout;
162162

163163
extern struct key_type key_type_keyring;
164164

include/linux/kmod.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ struct subprocess_info {
6464
int (*init)(struct subprocess_info *info, struct cred *new);
6565
void (*cleanup)(struct subprocess_info *info);
6666
void *data;
67-
};
67+
} __randomize_layout;
6868

6969
extern int
7070
call_usermodehelper(const char *path, char **argv, char **envp, int wait);

include/linux/kobject.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ struct kset {
172172
spinlock_t list_lock;
173173
struct kobject kobj;
174174
const struct kset_uevent_ops *uevent_ops;
175-
};
175+
} __randomize_layout;
176176

177177
extern void kset_init(struct kset *kset);
178178
extern int __must_check kset_register(struct kset *kset);

include/linux/lsm_hooks.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1876,7 +1876,7 @@ struct security_hook_heads {
18761876
struct list_head audit_rule_match;
18771877
struct list_head audit_rule_free;
18781878
#endif /* CONFIG_AUDIT */
1879-
};
1879+
} __randomize_layout;
18801880

18811881
/*
18821882
* Security module hook list structure.
@@ -1887,7 +1887,7 @@ struct security_hook_list {
18871887
struct list_head *head;
18881888
union security_list_options hook;
18891889
char *lsm;
1890-
};
1890+
} __randomize_layout;
18911891

18921892
/*
18931893
* Initializing a security_hook_list structure takes

include/linux/mm_types.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ struct vm_area_struct {
342342
struct mempolicy *vm_policy; /* NUMA policy for the VMA */
343343
#endif
344344
struct vm_userfaultfd_ctx vm_userfaultfd_ctx;
345-
};
345+
} __randomize_layout;
346346

347347
struct core_thread {
348348
struct task_struct *task;
@@ -500,7 +500,7 @@ struct mm_struct {
500500
atomic_long_t hugetlb_usage;
501501
#endif
502502
struct work_struct async_put_work;
503-
};
503+
} __randomize_layout;
504504

505505
extern struct mm_struct init_mm;
506506

include/linux/module.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ struct module_kobject {
4545
struct kobject *drivers_dir;
4646
struct module_param_attrs *mp;
4747
struct completion *kobj_completion;
48-
};
48+
} __randomize_layout;
4949

5050
struct module_attribute {
5151
struct attribute attr;
@@ -475,7 +475,7 @@ struct module {
475475
ctor_fn_t *ctors;
476476
unsigned int num_ctors;
477477
#endif
478-
} ____cacheline_aligned;
478+
} ____cacheline_aligned __randomize_layout;
479479
#ifndef MODULE_ARCH_INIT
480480
#define MODULE_ARCH_INIT {}
481481
#endif

include/linux/mount.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ struct vfsmount {
6767
struct dentry *mnt_root; /* root of the mounted tree */
6868
struct super_block *mnt_sb; /* pointer to superblock */
6969
int mnt_flags;
70-
};
70+
} __randomize_layout;
7171

7272
struct file; /* forward dec */
7373
struct path;

include/linux/msg.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ struct msg_queue {
2929
struct list_head q_messages;
3030
struct list_head q_receivers;
3131
struct list_head q_senders;
32-
};
32+
} __randomize_layout;
3333

3434
/* Helper routines for sys_msgsnd and sys_msgrcv */
3535
extern long do_msgsnd(int msqid, long mtype, void __user *mtext,

include/linux/path.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ struct vfsmount;
77
struct path {
88
struct vfsmount *mnt;
99
struct dentry *dentry;
10-
};
10+
} __randomize_layout;
1111

1212
extern void path_get(const struct path *);
1313
extern void path_put(const struct path *);

include/linux/pid_namespace.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ struct pid_namespace {
5252
int hide_pid;
5353
int reboot; /* group exit code if this pidns was rebooted */
5454
struct ns_common ns;
55-
};
55+
} __randomize_layout;
5656

5757
extern struct pid_namespace init_pid_ns;
5858

include/linux/proc_ns.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ struct proc_ns_operations {
2121
int (*install)(struct nsproxy *nsproxy, struct ns_common *ns);
2222
struct user_namespace *(*owner)(struct ns_common *ns);
2323
struct ns_common *(*get_parent)(struct ns_common *ns);
24-
};
24+
} __randomize_layout;
2525

2626
extern const struct proc_ns_operations netns_operations;
2727
extern const struct proc_ns_operations utsns_operations;

include/linux/sched.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ struct sched_rt_entity {
408408
/* rq "owned" by this entity/group: */
409409
struct rt_rq *my_q;
410410
#endif
411-
};
411+
} __randomize_layout;
412412

413413
struct sched_dl_entity {
414414
struct rb_node rb_node;

include/linux/sched/signal.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ struct signal_struct {
222222
struct mutex cred_guard_mutex; /* guard against foreign influences on
223223
* credential calculations
224224
* (notably. ptrace) */
225-
};
225+
} __randomize_layout;
226226

227227
/*
228228
* Bits in flags field of signal_struct.

include/linux/sem.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ struct sem_array {
2121
int sem_nsems; /* no. of semaphores in array */
2222
int complex_count; /* pending complex operations */
2323
unsigned int use_global_lock;/* >0: global lock required */
24-
};
24+
} __randomize_layout;
2525

2626
#ifdef CONFIG_SYSVIPC
2727

include/linux/shm.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ struct shmid_kernel /* private to the kernel */
2222
/* The task created the shm object. NULL if the task is dead. */
2323
struct task_struct *shm_creator;
2424
struct list_head shm_clist; /* list by creator */
25-
};
25+
} __randomize_layout;
2626

2727
/* shm_mode upper byte flags */
2828
#define SHM_DEST 01000 /* segment will be destroyed on last detach */

include/linux/sysctl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ struct ctl_table
117117
struct ctl_table_poll *poll;
118118
void *extra1;
119119
void *extra2;
120-
};
120+
} __randomize_layout;
121121

122122
struct ctl_node {
123123
struct rb_node node;

0 commit comments

Comments
 (0)