Re: [PATCH] bcache: make kobj_type structures constant

From: Thomas Weißschuh
Date: Tue Feb 14 2023 - 10:22:02 EST


On Tue, Feb 14, 2023 at 05:51:09PM +0800, Coly Li wrote:
>
>
> > 2023年2月14日 11:13,Thomas Weißschuh <linux@xxxxxxxxxxxxxx> 写道:
> >
> > Since commit ee6d3dd4ed48 ("driver core: make kobj_type constant.")
> > the driver core allows the usage of const struct kobj_type.
> >
> > Take advantage of this to constify the structure definitions to prevent
> > modification at runtime.
> >
>
> How the const structure definition can prevent modification at run time?

It will be put into .rodata instead of .data by the compiler.
The .rodata section is mapped as read-only via the pagetable.

See Documentation/security/self-protection.rst
"Function pointers and sensitive variables must not be writable".

Thomas

> Thanks.
>
> Coly Li
>
>
> > Signed-off-by: Thomas Weißschuh <linux@xxxxxxxxxxxxxx>
> > ---
> > drivers/md/bcache/bcache.h | 10 +++++-----
> > drivers/md/bcache/sysfs.h | 2 +-
> > 2 files changed, 6 insertions(+), 6 deletions(-)
> >
> > diff --git a/drivers/md/bcache/bcache.h b/drivers/md/bcache/bcache.h
> > index aebb7ef10e63..a522f4f1f992 100644
> > --- a/drivers/md/bcache/bcache.h
> > +++ b/drivers/md/bcache/bcache.h
> > @@ -1004,11 +1004,11 @@ extern struct workqueue_struct *bch_flush_wq;
> > extern struct mutex bch_register_lock;
> > extern struct list_head bch_cache_sets;
> >
> > -extern struct kobj_type bch_cached_dev_ktype;
> > -extern struct kobj_type bch_flash_dev_ktype;
> > -extern struct kobj_type bch_cache_set_ktype;
> > -extern struct kobj_type bch_cache_set_internal_ktype;
> > -extern struct kobj_type bch_cache_ktype;
> > +extern const struct kobj_type bch_cached_dev_ktype;
> > +extern const struct kobj_type bch_flash_dev_ktype;
> > +extern const struct kobj_type bch_cache_set_ktype;
> > +extern const struct kobj_type bch_cache_set_internal_ktype;
> > +extern const struct kobj_type bch_cache_ktype;
> >
> > void bch_cached_dev_release(struct kobject *kobj);
> > void bch_flash_dev_release(struct kobject *kobj);
> > diff --git a/drivers/md/bcache/sysfs.h b/drivers/md/bcache/sysfs.h
> > index a2ff6447b699..65b8bd975ab1 100644
> > --- a/drivers/md/bcache/sysfs.h
> > +++ b/drivers/md/bcache/sysfs.h
> > @@ -3,7 +3,7 @@
> > #define _BCACHE_SYSFS_H_
> >
> > #define KTYPE(type) \
> > -struct kobj_type type ## _ktype = { \
> > +const struct kobj_type type ## _ktype = { \
> > .release = type ## _release, \
> > .sysfs_ops = &((const struct sysfs_ops) { \
> > .show = type ## _show, \
> >
> > ---
> > base-commit: f6feea56f66d34259c4222fa02e8171c4f2673d1
> > change-id: 20230214-kobj_type-bcache-6d2bd129b0fa
> >
> > Best regards,
> > --
> > Thomas Weißschuh <linux@xxxxxxxxxxxxxx>
> >
>