linux-security-module.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] CIPSO: Fix unaligned memory access in cipso_v4_gentag_hdr
@ 2021-03-04 21:00 Seergey Nazarov
  2021-03-04 21:17 ` Ondrej Mosnacek
  0 siblings, 1 reply; 5+ messages in thread
From: Seergey Nazarov @ 2021-03-04 21:00 UTC (permalink / raw)
  To: linux-security-module; +Cc: paul

We need to use put_unaligned when writing 32-bit DOI value
in cipso_v4_gentag_hdr to avoid unaligned memory access.

Signed-off-by: Sergey Nazarov <s-nazarov@yandex.ru>
---
 net/ipv4/cipso_ipv4.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/net/ipv4/cipso_ipv4.c b/net/ipv4/cipso_ipv4.c
index 471d33a..28dfe40 100644
--- a/net/ipv4/cipso_ipv4.c
+++ b/net/ipv4/cipso_ipv4.c
@@ -1162,7 +1162,7 @@ static void cipso_v4_gentag_hdr(const struct
cipso_v4_doi *doi_def,
 {
 	buf[0] = IPOPT_CIPSO;
 	buf[1] = CIPSO_V4_HDR_LEN + len;
-	*(__be32 *)&buf[2] = htonl(doi_def->doi);
+	put_unaligned_be32(doi_def->doi, (__be32 *)&buf[2]);
 }
 
 /**
-- 
1.8.3.1



^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH] CIPSO: Fix unaligned memory access in cipso_v4_gentag_hdr
  2021-03-04 21:00 [PATCH] CIPSO: Fix unaligned memory access in cipso_v4_gentag_hdr Seergey Nazarov
@ 2021-03-04 21:17 ` Ondrej Mosnacek
  2021-03-04 21:45   ` Paul Moore
  2021-03-05  7:28   ` Seergey Nazarov
  0 siblings, 2 replies; 5+ messages in thread
From: Ondrej Mosnacek @ 2021-03-04 21:17 UTC (permalink / raw)
  To: Seergey Nazarov; +Cc: Linux Security Module list, Paul Moore

On Thu, Mar 4, 2021 at 10:09 PM Seergey Nazarov <s-nazarov@yandex.ru> wrote:
> We need to use put_unaligned when writing 32-bit DOI value
> in cipso_v4_gentag_hdr to avoid unaligned memory access.
>
> Signed-off-by: Sergey Nazarov <s-nazarov@yandex.ru>
> ---
>  net/ipv4/cipso_ipv4.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/net/ipv4/cipso_ipv4.c b/net/ipv4/cipso_ipv4.c
> index 471d33a..28dfe40 100644
> --- a/net/ipv4/cipso_ipv4.c
> +++ b/net/ipv4/cipso_ipv4.c
> @@ -1162,7 +1162,7 @@ static void cipso_v4_gentag_hdr(const struct
> cipso_v4_doi *doi_def,
>  {
>         buf[0] = IPOPT_CIPSO;
>         buf[1] = CIPSO_V4_HDR_LEN + len;
> -       *(__be32 *)&buf[2] = htonl(doi_def->doi);
> +       put_unaligned_be32(doi_def->doi, (__be32 *)&buf[2]);

I think you can now also drop the cast, since put_unaligned_be32()
expects a void *.

>  }
>
>  /**
> --
> 1.8.3.1
>
>

-- 
Ondrej Mosnacek
Software Engineer, Linux Security - SELinux kernel
Red Hat, Inc.


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] CIPSO: Fix unaligned memory access in cipso_v4_gentag_hdr
  2021-03-04 21:17 ` Ondrej Mosnacek
@ 2021-03-04 21:45   ` Paul Moore
  2021-03-05  7:40     ` Seergey Nazarov
  2021-03-05  7:28   ` Seergey Nazarov
  1 sibling, 1 reply; 5+ messages in thread
From: Paul Moore @ 2021-03-04 21:45 UTC (permalink / raw)
  To: Seergey Nazarov; +Cc: Linux Security Module list, Ondrej Mosnacek

On Thu, Mar 4, 2021 at 4:17 PM Ondrej Mosnacek <omosnace@redhat.com> wrote:
> On Thu, Mar 4, 2021 at 10:09 PM Seergey Nazarov <s-nazarov@yandex.ru> wrote:
> > We need to use put_unaligned when writing 32-bit DOI value
> > in cipso_v4_gentag_hdr to avoid unaligned memory access.
> >
> > Signed-off-by: Sergey Nazarov <s-nazarov@yandex.ru>
> > ---
> >  net/ipv4/cipso_ipv4.c | 2 +-
> >  1 file changed, 1 insertion(+), 1 deletion(-)
> >
> > diff --git a/net/ipv4/cipso_ipv4.c b/net/ipv4/cipso_ipv4.c
> > index 471d33a..28dfe40 100644
> > --- a/net/ipv4/cipso_ipv4.c
> > +++ b/net/ipv4/cipso_ipv4.c
> > @@ -1162,7 +1162,7 @@ static void cipso_v4_gentag_hdr(const struct
> > cipso_v4_doi *doi_def,
> >  {
> >         buf[0] = IPOPT_CIPSO;
> >         buf[1] = CIPSO_V4_HDR_LEN + len;
> > -       *(__be32 *)&buf[2] = htonl(doi_def->doi);
> > +       put_unaligned_be32(doi_def->doi, (__be32 *)&buf[2]);
>
> I think you can now also drop the cast, since put_unaligned_be32()
> expects a void *.

Yes, it shouldn't be necessary.

You should also send this to netdev as NetLabel code lives under net/
and goes to Linus via the netdev tree.

Also, looking at the code, there are a few similar spots you should
probably fix while you are revising this patch:

* the short/__be16 assignment in cipso_v4_map_cat_enum_hton()
* the short/__be16 assignments in cipso_v4_map_cat_rng_hton()
* the long/__be32 assignment in calipso_genopt()

-- 
paul moore
www.paul-moore.com

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] CIPSO: Fix unaligned memory access in cipso_v4_gentag_hdr
  2021-03-04 21:17 ` Ondrej Mosnacek
  2021-03-04 21:45   ` Paul Moore
@ 2021-03-05  7:28   ` Seergey Nazarov
  1 sibling, 0 replies; 5+ messages in thread
From: Seergey Nazarov @ 2021-03-05  7:28 UTC (permalink / raw)
  To: Ondrej Mosnacek; +Cc: Linux Security Module list, Paul Moore

В Чт, 04/03/2021 в 22:17 +0100, Ondrej Mosnacek пишет:
> On Thu, Mar 4, 2021 at 10:09 PM Seergey Nazarov <s-nazarov@yandex.ru>
> wrote:
> > We need to use put_unaligned when writing 32-bit DOI value
> > in cipso_v4_gentag_hdr to avoid unaligned memory access.
> > 
> > Signed-off-by: Sergey Nazarov <s-nazarov@yandex.ru>
> > ---
> >  net/ipv4/cipso_ipv4.c | 2 +-
> >  1 file changed, 1 insertion(+), 1 deletion(-)
> > 
> > diff --git a/net/ipv4/cipso_ipv4.c b/net/ipv4/cipso_ipv4.c
> > index 471d33a..28dfe40 100644
> > --- a/net/ipv4/cipso_ipv4.c
> > +++ b/net/ipv4/cipso_ipv4.c
> > @@ -1162,7 +1162,7 @@ static void cipso_v4_gentag_hdr(const struct
> > cipso_v4_doi *doi_def,
> >  {
> >         buf[0] = IPOPT_CIPSO;
> >         buf[1] = CIPSO_V4_HDR_LEN + len;
> > -       *(__be32 *)&buf[2] = htonl(doi_def->doi);
> > +       put_unaligned_be32(doi_def->doi, (__be32 *)&buf[2]);
> 
> I think you can now also drop the cast, since put_unaligned_be32()
> expects a void *.
> 
> >  }
> > 
> >  /**
> > --
> > 1.8.3.1
> > 
> > 
> 
> 
You are right, thanks!


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] CIPSO: Fix unaligned memory access in cipso_v4_gentag_hdr
  2021-03-04 21:45   ` Paul Moore
@ 2021-03-05  7:40     ` Seergey Nazarov
  0 siblings, 0 replies; 5+ messages in thread
From: Seergey Nazarov @ 2021-03-05  7:40 UTC (permalink / raw)
  To: Paul Moore; +Cc: Linux Security Module list, Ondrej Mosnacek

В Чт, 04/03/2021 в 16:45 -0500, Paul Moore пишет:
> On Thu, Mar 4, 2021 at 4:17 PM Ondrej Mosnacek <omosnace@redhat.com>
> wrote:
> > On Thu, Mar 4, 2021 at 10:09 PM Seergey Nazarov <
> > s-nazarov@yandex.ru> wrote:
> > > We need to use put_unaligned when writing 32-bit DOI value
> > > in cipso_v4_gentag_hdr to avoid unaligned memory access.
> > > 
> > > Signed-off-by: Sergey Nazarov <s-nazarov@yandex.ru>
> > > ---
> > >  net/ipv4/cipso_ipv4.c | 2 +-
> > >  1 file changed, 1 insertion(+), 1 deletion(-)
> > > 
> > > diff --git a/net/ipv4/cipso_ipv4.c b/net/ipv4/cipso_ipv4.c
> > > index 471d33a..28dfe40 100644
> > > --- a/net/ipv4/cipso_ipv4.c
> > > +++ b/net/ipv4/cipso_ipv4.c
> > > @@ -1162,7 +1162,7 @@ static void cipso_v4_gentag_hdr(const
> > > struct
> > > cipso_v4_doi *doi_def,
> > >  {
> > >         buf[0] = IPOPT_CIPSO;
> > >         buf[1] = CIPSO_V4_HDR_LEN + len;
> > > -       *(__be32 *)&buf[2] = htonl(doi_def->doi);
> > > +       put_unaligned_be32(doi_def->doi, (__be32 *)&buf[2]);
> > 
> > I think you can now also drop the cast, since put_unaligned_be32()
> > expects a void *.
> 
> Yes, it shouldn't be necessary.
> 
> You should also send this to netdev as NetLabel code lives under net/
> and goes to Linus via the netdev tree.
> 
> Also, looking at the code, there are a few similar spots you should
> probably fix while you are revising this patch:
> 
> * the short/__be16 assignment in cipso_v4_map_cat_enum_hton()
> * the short/__be16 assignments in cipso_v4_map_cat_rng_hton()
> * the long/__be32 assignment incalipso_genopt()
> 
Yes, I looked it.
In cipso_v4_map_cat_enum_hton, cipso_v4_map_cat_rng_hton access
address aligned by 2, so it's okay.
In cipso_v4_gentag_loc, cipso_v4_parsetag_loc access address is
CIPSO_V4_HDR_LEN(6) + 2, so it's okay too.
In calipso_genopt access address is 4n + 2 + 2, it's okay too.



^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2021-03-05  7:45 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-03-04 21:00 [PATCH] CIPSO: Fix unaligned memory access in cipso_v4_gentag_hdr Seergey Nazarov
2021-03-04 21:17 ` Ondrej Mosnacek
2021-03-04 21:45   ` Paul Moore
2021-03-05  7:40     ` Seergey Nazarov
2021-03-05  7:28   ` Seergey Nazarov

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).