diff mbox series

[2/4] libsepol/cil: Fix potential undefined shifts

Message ID 20211008211031.393884-2-jwcart2@gmail.com (mailing list archive)
State Accepted
Headers show
Series [1/4] libsepol: Fix potential undefined shifts | expand

Commit Message

James Carter Oct. 8, 2021, 9:10 p.m. UTC
An expression of the form "1 << x" is undefined if x == 31 because
the "1" is an int and cannot be left shifted by 31.

Instead, use "UINT32_C(1) << x" which will be an unsigned int of
at least 32 bits.

This bug was found by the secilc-fuzzer.

Signed-off-by: James Carter <jwcart2@gmail.com>
---
 libsepol/cil/src/cil_binary.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

Comments

Nicolas Iooss Oct. 10, 2021, 8:40 p.m. UTC | #1
On Fri, Oct 8, 2021 at 11:10 PM James Carter <jwcart2@gmail.com> wrote:
>
> An expression of the form "1 << x" is undefined if x == 31 because
> the "1" is an int and cannot be left shifted by 31.
>
> Instead, use "UINT32_C(1) << x" which will be an unsigned int of
> at least 32 bits.
>
> This bug was found by the secilc-fuzzer.
>
> Signed-off-by: James Carter <jwcart2@gmail.com>
> ---
>  libsepol/cil/src/cil_binary.c | 6 +++---
>  1 file changed, 3 insertions(+), 3 deletions(-)
>
> diff --git a/libsepol/cil/src/cil_binary.c b/libsepol/cil/src/cil_binary.c
> index ec5f01e5..34dc63c7 100644
> --- a/libsepol/cil/src/cil_binary.c
> +++ b/libsepol/cil/src/cil_binary.c
> @@ -1225,7 +1225,7 @@ int __perm_str_to_datum(char *perm_str, class_datum_t *sepol_class, uint32_t *da
>                         goto exit;
>                 }
>         }
> -       *datum |= 1 << (sepol_perm->s.value - 1);
> +       *datum |= UINT32_C(1) << (sepol_perm->s.value - 1);
>
>         return SEPOL_OK;
>
> @@ -1523,7 +1523,7 @@ int cil_avrule_to_policydb(policydb_t *pdb, const struct cil_db *db, struct cil_
>  /* index of the u32 containing the permission */
>  #define XPERM_IDX(x) (x >> 5)
>  /* set bits 0 through x-1 within the u32 */
> -#define XPERM_SETBITS(x) ((1U << (x & 0x1f)) - 1)
> +#define XPERM_SETBITS(x) (UINT32_C(1) << (x & 0x1f)) - 1)

There is a missing parenthesis here: "UINT32_C(1)" -> "(UINT32_C(1)"

Other than this, the 4 patches look good to me:

Acked-by: Nicolas Iooss <nicolas.iooss@m4x.org>

Thanks!
Nicolas

>  /* low value for this u32 */
>  #define XPERM_LOW(x) (x << 5)
>  /* high value for this u32 */
> @@ -4760,7 +4760,7 @@ static struct cil_list *cil_classperms_from_sepol(policydb_t *pdb, uint16_t clas
>         cil_list_init(&cp->perms, CIL_PERM);
>         for (i = 0; i < sepol_class->permissions.nprim; i++) {
>                 struct cil_perm *perm;
> -               if ((data & (1 << i)) == 0) continue;
> +               if ((data & (UINT32_C(1) << i)) == 0) continue;
>                 perm = perm_value_to_cil[class][i+1];
>                 if (!perm) goto exit;
>                 cil_list_append(cp->perms, CIL_PERM, perm);
> --
> 2.31.1
>
James Carter Oct. 12, 2021, 5:51 p.m. UTC | #2
On Sun, Oct 10, 2021 at 4:40 PM Nicolas Iooss <nicolas.iooss@m4x.org> wrote:
>
> On Fri, Oct 8, 2021 at 11:10 PM James Carter <jwcart2@gmail.com> wrote:
> >
> > An expression of the form "1 << x" is undefined if x == 31 because
> > the "1" is an int and cannot be left shifted by 31.
> >
> > Instead, use "UINT32_C(1) << x" which will be an unsigned int of
> > at least 32 bits.
> >
> > This bug was found by the secilc-fuzzer.
> >
> > Signed-off-by: James Carter <jwcart2@gmail.com>
> > ---
> >  libsepol/cil/src/cil_binary.c | 6 +++---
> >  1 file changed, 3 insertions(+), 3 deletions(-)
> >
> > diff --git a/libsepol/cil/src/cil_binary.c b/libsepol/cil/src/cil_binary.c
> > index ec5f01e5..34dc63c7 100644
> > --- a/libsepol/cil/src/cil_binary.c
> > +++ b/libsepol/cil/src/cil_binary.c
> > @@ -1225,7 +1225,7 @@ int __perm_str_to_datum(char *perm_str, class_datum_t *sepol_class, uint32_t *da
> >                         goto exit;
> >                 }
> >         }
> > -       *datum |= 1 << (sepol_perm->s.value - 1);
> > +       *datum |= UINT32_C(1) << (sepol_perm->s.value - 1);
> >
> >         return SEPOL_OK;
> >
> > @@ -1523,7 +1523,7 @@ int cil_avrule_to_policydb(policydb_t *pdb, const struct cil_db *db, struct cil_
> >  /* index of the u32 containing the permission */
> >  #define XPERM_IDX(x) (x >> 5)
> >  /* set bits 0 through x-1 within the u32 */
> > -#define XPERM_SETBITS(x) ((1U << (x & 0x1f)) - 1)
> > +#define XPERM_SETBITS(x) (UINT32_C(1) << (x & 0x1f)) - 1)
>
> There is a missing parenthesis here: "UINT32_C(1)" -> "(UINT32_C(1)"
>
> Other than this, the 4 patches look good to me:
>
> Acked-by: Nicolas Iooss <nicolas.iooss@m4x.org>
>

I fixed the missing parenthesis and merged these patches.
Thanks,
Jim

> Thanks!
> Nicolas
>
> >  /* low value for this u32 */
> >  #define XPERM_LOW(x) (x << 5)
> >  /* high value for this u32 */
> > @@ -4760,7 +4760,7 @@ static struct cil_list *cil_classperms_from_sepol(policydb_t *pdb, uint16_t clas
> >         cil_list_init(&cp->perms, CIL_PERM);
> >         for (i = 0; i < sepol_class->permissions.nprim; i++) {
> >                 struct cil_perm *perm;
> > -               if ((data & (1 << i)) == 0) continue;
> > +               if ((data & (UINT32_C(1) << i)) == 0) continue;
> >                 perm = perm_value_to_cil[class][i+1];
> >                 if (!perm) goto exit;
> >                 cil_list_append(cp->perms, CIL_PERM, perm);
> > --
> > 2.31.1
> >
>
diff mbox series

Patch

diff --git a/libsepol/cil/src/cil_binary.c b/libsepol/cil/src/cil_binary.c
index ec5f01e5..34dc63c7 100644
--- a/libsepol/cil/src/cil_binary.c
+++ b/libsepol/cil/src/cil_binary.c
@@ -1225,7 +1225,7 @@  int __perm_str_to_datum(char *perm_str, class_datum_t *sepol_class, uint32_t *da
 			goto exit;
 		}
 	}
-	*datum |= 1 << (sepol_perm->s.value - 1);
+	*datum |= UINT32_C(1) << (sepol_perm->s.value - 1);
 
 	return SEPOL_OK;
 
@@ -1523,7 +1523,7 @@  int cil_avrule_to_policydb(policydb_t *pdb, const struct cil_db *db, struct cil_
 /* index of the u32 containing the permission */
 #define XPERM_IDX(x) (x >> 5)
 /* set bits 0 through x-1 within the u32 */
-#define XPERM_SETBITS(x) ((1U << (x & 0x1f)) - 1)
+#define XPERM_SETBITS(x) (UINT32_C(1) << (x & 0x1f)) - 1)
 /* low value for this u32 */
 #define XPERM_LOW(x) (x << 5)
 /* high value for this u32 */
@@ -4760,7 +4760,7 @@  static struct cil_list *cil_classperms_from_sepol(policydb_t *pdb, uint16_t clas
 	cil_list_init(&cp->perms, CIL_PERM);
 	for (i = 0; i < sepol_class->permissions.nprim; i++) {
 		struct cil_perm *perm;
-		if ((data & (1 << i)) == 0) continue;
+		if ((data & (UINT32_C(1) << i)) == 0) continue;
 		perm = perm_value_to_cil[class][i+1];
 		if (!perm) goto exit;
 		cil_list_append(cp->perms, CIL_PERM, perm);