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 <keescook@chromium.org>
1 parent ffa47aa commit 3859a27

38 files changed

Lines changed: 57 additions & 56 deletions

‎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

0 commit comments

Comments
 (0)