Skip to content

Commit ed66f99

Browse files
committed
module: Refactor section attr into bin attribute
In order to gain access to the open file's f_cred for kallsym visibility permission checks, refactor the module section attributes to use the bin_attribute instead of attribute interface. Additionally removes the redundant "name" struct member. Cc: [email protected] Reviewed-by: Greg Kroah-Hartman <[email protected]> Tested-by: Jessica Yu <[email protected]> Acked-by: Jessica Yu <[email protected]> Signed-off-by: Kees Cook <[email protected]>
1 parent 1602518 commit ed66f99

File tree

1 file changed

+24
-21
lines changed

1 file changed

+24
-21
lines changed

kernel/module.c

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1510,8 +1510,7 @@ static inline bool sect_empty(const Elf_Shdr *sect)
15101510
}
15111511

15121512
struct module_sect_attr {
1513-
struct module_attribute mattr;
1514-
char *name;
1513+
struct bin_attribute battr;
15151514
unsigned long address;
15161515
};
15171516

@@ -1521,11 +1520,16 @@ struct module_sect_attrs {
15211520
struct module_sect_attr attrs[];
15221521
};
15231522

1524-
static ssize_t module_sect_show(struct module_attribute *mattr,
1525-
struct module_kobject *mk, char *buf)
1523+
static ssize_t module_sect_read(struct file *file, struct kobject *kobj,
1524+
struct bin_attribute *battr,
1525+
char *buf, loff_t pos, size_t count)
15261526
{
15271527
struct module_sect_attr *sattr =
1528-
container_of(mattr, struct module_sect_attr, mattr);
1528+
container_of(battr, struct module_sect_attr, battr);
1529+
1530+
if (pos != 0)
1531+
return -EINVAL;
1532+
15291533
return sprintf(buf, "0x%px\n", kptr_restrict < 2 ?
15301534
(void *)sattr->address : NULL);
15311535
}
@@ -1535,7 +1539,7 @@ static void free_sect_attrs(struct module_sect_attrs *sect_attrs)
15351539
unsigned int section;
15361540

15371541
for (section = 0; section < sect_attrs->nsections; section++)
1538-
kfree(sect_attrs->attrs[section].name);
1542+
kfree(sect_attrs->attrs[section].battr.attr.name);
15391543
kfree(sect_attrs);
15401544
}
15411545

@@ -1544,42 +1548,41 @@ static void add_sect_attrs(struct module *mod, const struct load_info *info)
15441548
unsigned int nloaded = 0, i, size[2];
15451549
struct module_sect_attrs *sect_attrs;
15461550
struct module_sect_attr *sattr;
1547-
struct attribute **gattr;
1551+
struct bin_attribute **gattr;
15481552

15491553
/* Count loaded sections and allocate structures */
15501554
for (i = 0; i < info->hdr->e_shnum; i++)
15511555
if (!sect_empty(&info->sechdrs[i]))
15521556
nloaded++;
15531557
size[0] = ALIGN(struct_size(sect_attrs, attrs, nloaded),
1554-
sizeof(sect_attrs->grp.attrs[0]));
1555-
size[1] = (nloaded + 1) * sizeof(sect_attrs->grp.attrs[0]);
1558+
sizeof(sect_attrs->grp.bin_attrs[0]));
1559+
size[1] = (nloaded + 1) * sizeof(sect_attrs->grp.bin_attrs[0]);
15561560
sect_attrs = kzalloc(size[0] + size[1], GFP_KERNEL);
15571561
if (sect_attrs == NULL)
15581562
return;
15591563

15601564
/* Setup section attributes. */
15611565
sect_attrs->grp.name = "sections";
1562-
sect_attrs->grp.attrs = (void *)sect_attrs + size[0];
1566+
sect_attrs->grp.bin_attrs = (void *)sect_attrs + size[0];
15631567

15641568
sect_attrs->nsections = 0;
15651569
sattr = &sect_attrs->attrs[0];
1566-
gattr = &sect_attrs->grp.attrs[0];
1570+
gattr = &sect_attrs->grp.bin_attrs[0];
15671571
for (i = 0; i < info->hdr->e_shnum; i++) {
15681572
Elf_Shdr *sec = &info->sechdrs[i];
15691573
if (sect_empty(sec))
15701574
continue;
1575+
sysfs_bin_attr_init(&sattr->battr);
15711576
sattr->address = sec->sh_addr;
1572-
sattr->name = kstrdup(info->secstrings + sec->sh_name,
1573-
GFP_KERNEL);
1574-
if (sattr->name == NULL)
1577+
sattr->battr.attr.name =
1578+
kstrdup(info->secstrings + sec->sh_name, GFP_KERNEL);
1579+
if (sattr->battr.attr.name == NULL)
15751580
goto out;
15761581
sect_attrs->nsections++;
1577-
sysfs_attr_init(&sattr->mattr.attr);
1578-
sattr->mattr.show = module_sect_show;
1579-
sattr->mattr.store = NULL;
1580-
sattr->mattr.attr.name = sattr->name;
1581-
sattr->mattr.attr.mode = S_IRUSR;
1582-
*(gattr++) = &(sattr++)->mattr.attr;
1582+
sattr->battr.read = module_sect_read;
1583+
sattr->battr.size = 3 /* "0x", "\n" */ + (BITS_PER_LONG / 4);
1584+
sattr->battr.attr.mode = 0400;
1585+
*(gattr++) = &(sattr++)->battr;
15831586
}
15841587
*gattr = NULL;
15851588

@@ -1669,7 +1672,7 @@ static void add_notes_attrs(struct module *mod, const struct load_info *info)
16691672
continue;
16701673
if (info->sechdrs[i].sh_type == SHT_NOTE) {
16711674
sysfs_bin_attr_init(nattr);
1672-
nattr->attr.name = mod->sect_attrs->attrs[loaded].name;
1675+
nattr->attr.name = mod->sect_attrs->attrs[loaded].battr.attr.name;
16731676
nattr->attr.mode = S_IRUGO;
16741677
nattr->size = info->sechdrs[i].sh_size;
16751678
nattr->private = (void *) info->sechdrs[i].sh_addr;

0 commit comments

Comments
 (0)