2023-06-03 14:54:16

by Demi Marie Obenour

[permalink] [raw]
Subject: [PATCH v2 1/6] device-mapper: Check that target specs are sufficiently aligned

Otherwise subsequent code will dereference a misaligned
`struct dm_target_spec *`, which is undefined behavior.

Signed-off-by: Demi Marie Obenour <[email protected]>
Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
Cc: [email protected]
---
drivers/md/dm-ioctl.c | 7 +++++++
1 file changed, 7 insertions(+)

diff --git a/drivers/md/dm-ioctl.c b/drivers/md/dm-ioctl.c
index cc77cf3d410921432eb0c62cdede7d55b9aa674a..34fa74c6a70db8aa67aaba3f6a2fc4f38ef736bc 100644
--- a/drivers/md/dm-ioctl.c
+++ b/drivers/md/dm-ioctl.c
@@ -1394,6 +1394,13 @@ static inline fmode_t get_mode(struct dm_ioctl *param)
static int next_target(struct dm_target_spec *last, uint32_t next, void *end,
struct dm_target_spec **spec, char **target_params)
{
+ static_assert(_Alignof(struct dm_target_spec) <= 8,
+ "struct dm_target_spec has excessive alignment requirements");
+ if (next % 8) {
+ DMERR("Next target spec (offset %u) is not 8-byte aligned", next);
+ return -EINVAL;
+ }
+
*spec = (struct dm_target_spec *) ((unsigned char *) last + next);
*target_params = (char *) (*spec + 1);

--
Sincerely,
Demi Marie Obenour (she/her/hers)
Invisible Things Lab



2023-06-22 16:54:48

by Mike Snitzer

[permalink] [raw]
Subject: Re: [PATCH v2 1/6] device-mapper: Check that target specs are sufficiently aligned

On Sat, Jun 03 2023 at 10:52P -0400,
Demi Marie Obenour <[email protected]> wrote:

> Otherwise subsequent code will dereference a misaligned
> `struct dm_target_spec *`, which is undefined behavior.
>
> Signed-off-by: Demi Marie Obenour <[email protected]>
> Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
> Cc: [email protected]
> ---
> drivers/md/dm-ioctl.c | 7 +++++++
> 1 file changed, 7 insertions(+)
>
> diff --git a/drivers/md/dm-ioctl.c b/drivers/md/dm-ioctl.c
> index cc77cf3d410921432eb0c62cdede7d55b9aa674a..34fa74c6a70db8aa67aaba3f6a2fc4f38ef736bc 100644
> --- a/drivers/md/dm-ioctl.c
> +++ b/drivers/md/dm-ioctl.c
> @@ -1394,6 +1394,13 @@ static inline fmode_t get_mode(struct dm_ioctl *param)
> static int next_target(struct dm_target_spec *last, uint32_t next, void *end,
> struct dm_target_spec **spec, char **target_params)
> {
> + static_assert(_Alignof(struct dm_target_spec) <= 8,
> + "struct dm_target_spec has excessive alignment requirements");

Really not sure what you mean by "has excessive alignment requirements"...

> + if (next % 8) {
> + DMERR("Next target spec (offset %u) is not 8-byte aligned", next);
> + return -EINVAL;
> + }
> +
> *spec = (struct dm_target_spec *) ((unsigned char *) last + next);
> *target_params = (char *) (*spec + 1);
>

But this patch and patches 2 and 3 need more review. I'd like Mikulas to review.

I did pick up patches 4-6 for the upcoming 6.5 merge window.

Note, please prefix with "dm ioctl" instead of "device-mapper".

(I just switched my "dm" prefix to "dm ioctl" and forced update on the
dm-6.5 branch, so the commit I referenced earlier for your version
copy patch is now here:
https://git.kernel.org/pub/scm/linux/kernel/git/device-mapper/linux-dm.git/commit/?h=dm-6.5&id=a5a3de762b3ae8959347928843c12502b1b23163
)

Mike

2023-06-22 17:40:47

by Mikulas Patocka

[permalink] [raw]
Subject: Re: [dm-devel] [PATCH v2 1/6] device-mapper: Check that target specs are sufficiently aligned



On Sat, 3 Jun 2023, Demi Marie Obenour wrote:

> Otherwise subsequent code will dereference a misaligned
> `struct dm_target_spec *`, which is undefined behavior.
>
> Signed-off-by: Demi Marie Obenour <[email protected]>
> Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
> Cc: [email protected]
> ---
> drivers/md/dm-ioctl.c | 7 +++++++
> 1 file changed, 7 insertions(+)
>
> diff --git a/drivers/md/dm-ioctl.c b/drivers/md/dm-ioctl.c
> index cc77cf3d410921432eb0c62cdede7d55b9aa674a..34fa74c6a70db8aa67aaba3f6a2fc4f38ef736bc 100644
> --- a/drivers/md/dm-ioctl.c
> +++ b/drivers/md/dm-ioctl.c
> @@ -1394,6 +1394,13 @@ static inline fmode_t get_mode(struct dm_ioctl *param)
> static int next_target(struct dm_target_spec *last, uint32_t next, void *end,
> struct dm_target_spec **spec, char **target_params)
> {
> + static_assert(_Alignof(struct dm_target_spec) <= 8,
> + "struct dm_target_spec has excessive alignment requirements");
> + if (next % 8) {
> + DMERR("Next target spec (offset %u) is not 8-byte aligned", next);
> + return -EINVAL;
> + }
> +
> *spec = (struct dm_target_spec *) ((unsigned char *) last + next);
> *target_params = (char *) (*spec + 1);
>
> --
> Sincerely,
> Demi Marie Obenour (she/her/hers)
> Invisible Things Lab

Hi

Some architectures (such as 32-bit x86) specify that the alignment of
64-bit integers is only 4-byte. This could in theory break old userspace
code that only uses 4-byte alignment. I would change "next % 8" to "next %
__alignof__(struct dm_target_spec)".

I think that there is no need to backport this patch series to the stable
kernels because the bugs that it fixes may only be exploited by the user
with CAP_SYS_ADMIN privilege. So, there is no security or reliability
problem being fixed.

Mikulas


2023-06-22 20:21:39

by Demi Marie Obenour

[permalink] [raw]
Subject: Re: [PATCH v2 1/6] device-mapper: Check that target specs are sufficiently aligned

On Thu, Jun 22, 2023 at 12:28:28PM -0400, Mike Snitzer wrote:
> On Sat, Jun 03 2023 at 10:52P -0400,
> Demi Marie Obenour <[email protected]> wrote:
>
> > Otherwise subsequent code will dereference a misaligned
> > `struct dm_target_spec *`, which is undefined behavior.
> >
> > Signed-off-by: Demi Marie Obenour <[email protected]>
> > Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
> > Cc: [email protected]
> > ---
> > drivers/md/dm-ioctl.c | 7 +++++++
> > 1 file changed, 7 insertions(+)
> >
> > diff --git a/drivers/md/dm-ioctl.c b/drivers/md/dm-ioctl.c
> > index cc77cf3d410921432eb0c62cdede7d55b9aa674a..34fa74c6a70db8aa67aaba3f6a2fc4f38ef736bc 100644
> > --- a/drivers/md/dm-ioctl.c
> > +++ b/drivers/md/dm-ioctl.c
> > @@ -1394,6 +1394,13 @@ static inline fmode_t get_mode(struct dm_ioctl *param)
> > static int next_target(struct dm_target_spec *last, uint32_t next, void *end,
> > struct dm_target_spec **spec, char **target_params)
> > {
> > + static_assert(_Alignof(struct dm_target_spec) <= 8,
> > + "struct dm_target_spec has excessive alignment requirements");
>
> Really not sure what you mean by "has excessive alignment requirements"...

This patch checks that struct dm_target_spec is 8-byte aligned. That is
okay if its alignment is 8 or less, but not if is 16 or more, so I added
a static assert to check that struct dm_target_spec indeed requires at
most 8-byte alignment. That said, “excessive alignment requirements” is
(as shown by you having to ask this question) a bad error message.
Would “must not require more than 8-byte alignment” be better?

> > + if (next % 8) {
> > + DMERR("Next target spec (offset %u) is not 8-byte aligned", next);
> > + return -EINVAL;
> > + }
> > +
> > *spec = (struct dm_target_spec *) ((unsigned char *) last + next);
> > *target_params = (char *) (*spec + 1);
> >
>
> But this patch and patches 2 and 3 need more review. I'd like Mikulas to review.
>
> I did pick up patches 4-6 for the upcoming 6.5 merge window.

Thanks!

> Note, please prefix with "dm ioctl" instead of "device-mapper".

Good to know, thanks! I have several additional patches written that
require patch 4. Should I send patches 1 through 3 in the same series
as well?
--
Sincerely,
Demi Marie Obenour (she/her/hers)
Invisible Things Lab


Attachments:
(No filename) (2.34 kB)
signature.asc (849.00 B)
Download all attachments

2023-06-22 20:52:35

by Demi Marie Obenour

[permalink] [raw]
Subject: Re: [dm-devel] [PATCH v2 1/6] device-mapper: Check that target specs are sufficiently aligned

On Thu, Jun 22, 2023 at 07:29:52PM +0200, Mikulas Patocka wrote:
>
>
> On Sat, 3 Jun 2023, Demi Marie Obenour wrote:
>
> > Otherwise subsequent code will dereference a misaligned
> > `struct dm_target_spec *`, which is undefined behavior.
> >
> > Signed-off-by: Demi Marie Obenour <[email protected]>
> > Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
> > Cc: [email protected]
> > ---
> > drivers/md/dm-ioctl.c | 7 +++++++
> > 1 file changed, 7 insertions(+)
> >
> > diff --git a/drivers/md/dm-ioctl.c b/drivers/md/dm-ioctl.c
> > index cc77cf3d410921432eb0c62cdede7d55b9aa674a..34fa74c6a70db8aa67aaba3f6a2fc4f38ef736bc 100644
> > --- a/drivers/md/dm-ioctl.c
> > +++ b/drivers/md/dm-ioctl.c
> > @@ -1394,6 +1394,13 @@ static inline fmode_t get_mode(struct dm_ioctl *param)
> > static int next_target(struct dm_target_spec *last, uint32_t next, void *end,
> > struct dm_target_spec **spec, char **target_params)
> > {
> > + static_assert(_Alignof(struct dm_target_spec) <= 8,
> > + "struct dm_target_spec has excessive alignment requirements");
> > + if (next % 8) {
> > + DMERR("Next target spec (offset %u) is not 8-byte aligned", next);
> > + return -EINVAL;
> > + }
> > +
> > *spec = (struct dm_target_spec *) ((unsigned char *) last + next);
> > *target_params = (char *) (*spec + 1);
> >
> > --
> > Sincerely,
> > Demi Marie Obenour (she/her/hers)
> > Invisible Things Lab
>
> Hi
>
> Some architectures (such as 32-bit x86) specify that the alignment of
> 64-bit integers is only 4-byte. This could in theory break old userspace
> code that only uses 4-byte alignment. I would change "next % 8" to "next %
> __alignof__(struct dm_target_spec)".

That’s fine, provided that the rest of the code is okay with 4-byte
alignment.

> I think that there is no need to backport this patch series to the stable
> kernels because the bugs that it fixes may only be exploited by the user
> with CAP_SYS_ADMIN privilege. So, there is no security or reliability
> problem being fixed.

I agree that there is no reliability problem, but with kernel lockdown
root → kernel is a security boundary, so fixes for memory unsafety
problems should still be backported IMO.
--
Sincerely,
Demi Marie Obenour (she/her/hers)
Invisible Things Lab


Attachments:
(No filename) (2.29 kB)
signature.asc (849.00 B)
Download all attachments

2023-06-22 23:51:17

by Mike Snitzer

[permalink] [raw]
Subject: Re: [PATCH v2 1/6] device-mapper: Check that target specs are sufficiently aligned

On Thu, Jun 22 2023 at 3:51P -0400,
Demi Marie Obenour <[email protected]> wrote:

> On Thu, Jun 22, 2023 at 12:28:28PM -0400, Mike Snitzer wrote:
> > On Sat, Jun 03 2023 at 10:52P -0400,
> > Demi Marie Obenour <[email protected]> wrote:
> >
> > > Otherwise subsequent code will dereference a misaligned
> > > `struct dm_target_spec *`, which is undefined behavior.
> > >
> > > Signed-off-by: Demi Marie Obenour <[email protected]>
> > > Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
> > > Cc: [email protected]
> > > ---
> > > drivers/md/dm-ioctl.c | 7 +++++++
> > > 1 file changed, 7 insertions(+)
> > >
> > > diff --git a/drivers/md/dm-ioctl.c b/drivers/md/dm-ioctl.c
> > > index cc77cf3d410921432eb0c62cdede7d55b9aa674a..34fa74c6a70db8aa67aaba3f6a2fc4f38ef736bc 100644
> > > --- a/drivers/md/dm-ioctl.c
> > > +++ b/drivers/md/dm-ioctl.c
> > > @@ -1394,6 +1394,13 @@ static inline fmode_t get_mode(struct dm_ioctl *param)
> > > static int next_target(struct dm_target_spec *last, uint32_t next, void *end,
> > > struct dm_target_spec **spec, char **target_params)
> > > {
> > > + static_assert(_Alignof(struct dm_target_spec) <= 8,
> > > + "struct dm_target_spec has excessive alignment requirements");
> >
> > Really not sure what you mean by "has excessive alignment requirements"...
>
> This patch checks that struct dm_target_spec is 8-byte aligned. That is
> okay if its alignment is 8 or less, but not if is 16 or more, so I added
> a static assert to check that struct dm_target_spec indeed requires at
> most 8-byte alignment. That said, “excessive alignment requirements” is
> (as shown by you having to ask this question) a bad error message.
> Would “must not require more than 8-byte alignment” be better?

Yes, that's better, I've updated it to use that.

> > > + if (next % 8) {
> > > + DMERR("Next target spec (offset %u) is not 8-byte aligned", next);
> > > + return -EINVAL;
> > > + }
> > > +
> > > *spec = (struct dm_target_spec *) ((unsigned char *) last + next);
> > > *target_params = (char *) (*spec + 1);
> > >
> >
> > But this patch and patches 2 and 3 need more review. I'd like Mikulas to review.
> >
> > I did pick up patches 4-6 for the upcoming 6.5 merge window.
>
> Thanks!
>
> > Note, please prefix with "dm ioctl" instead of "device-mapper".
>
> Good to know, thanks! I have several additional patches written that
> require patch 4. Should I send patches 1 through 3 in the same series
> as well?

I did end up picking up patches 1-3 and rebased so they are in front
of your patches 4-6 like you intended.

But I agree with Mikulas, I'm not seeing the point in tagging any of
these for stable@.

All commits are currently at the tip of dm-6.5, see:
https://git.kernel.org/pub/scm/linux/kernel/git/device-mapper/linux-dm.git/log/?h=dm-6.5

Mike