Skip to contentSkip to conversation

LLVMLinux: Patches to enable the kernel to be compiled with clang/LLVM

RFC PATCH Superseded [email protected] 4 participants 13 messages Sep 2, 2014

Subsystems (8)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover LLVMLinux: Patches to enable the kernel to be compiled with clang/LLVM

From: Behan Webster <[email protected]>

These patches remove the use of Variable Length Arrays In Structs (VLAIS) in crypto related code. Presented here for comments as a whole (since they all do the same thing in the same way). Once everyone is happy I will submit them individually to their appropriate maintainers.

The LLVMLinux project aims to fully build the Linux kernel using both gcc and clang (the C front end for the LLVM compiler infrastructure project).

Jan-Simon Möller (4):
crypto, dm: LLVMLinux: Remove VLAIS usage from dm-crypt
crypto: LLVMLinux: Remove VLAIS usage from crypto/hmac.c
crypto: LLVMLinux: Remove VLAIS usage from libcrc32c.c
crypto: LLVMLinux: Remove VLAIS usage from crypto/testmgr.c

Vinícius Tinti (2):
apparmor: LLVMLinux: Remove VLAIS
btrfs: LLVMLinux: Remove VLAIS

 crypto/hmac.c              | 27 +++++++++++++--------------
 crypto/testmgr.c           | 16 ++++++++--------
 drivers/md/dm-crypt.c      | 38 ++++++++++++++++++--------------------
 fs/btrfs/hash.c            | 18 +++++++++---------
 lib/libcrc32c.c            | 18 +++++++++---------
 security/apparmor/crypto.c | 19 +++++++++----------
 6 files changed, 66 insertions(+), 70 deletions(-)
Signature · 1 line
1.9.1
#1 lore raw

RFC PATCH 1/6 crypto, dm: LLVMLinux: Remove VLAIS usage from dm-crypt

From: Jan-Simon Möller <[email protected]>

The use of variable length arrays in structs (VLAIS) in the Linux Kernel code precludes the use of compilers which don't implement VLAIS (for instance the Clang compiler). This patch instead allocates the appropriate amount of memory using an char array.

struct shash_desc contains a flexible array member member ctx declared with CRYPTO_MINALIGN_ATTR, so sizeof(struct shash_desc) aligns the beginning of the array declared after struct shash_desc with long long.

No trailing padding is required because it is not a struct type that can be used in an array.

The CRYPTO_MINALIGN_ATTR is required so that desc is aligned with long long as would be the case for a struct containing a member with CRYPTO_MINALIGN_ATTR.

---
 drivers/md/dm-crypt.c | 38 ++++++++++++++++++--------------------
 1 file changed, 18 insertions(+), 20 deletions(-)
drivers/md/dm-crypt.c +18 -20
index cd15e08..4d940d3 100644
@@ -526,29 +526,28 @@ static int crypt_iv_lmk_one(struct crypt_config *cc, u8 *iv,
 			    u8 *data)
 {
 	struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
-	struct {
-		struct shash_desc desc;
-		char ctx[crypto_shash_descsize(lmk->hash_tfm)];
-	} sdesc;
+	char sdesc[sizeof(struct shash_desc)
+		+ crypto_shash_descsize(lmk->hash_tfm)] CRYPTO_MINALIGN_ATTR;
+	struct shash_desc *desc = (struct shash_desc *)sdesc;
 	struct md5_state md5state;
 	__le32 buf[4];
 	int i, r;
 
-	sdesc.desc.tfm = lmk->hash_tfm;
-	sdesc.desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP;
+	desc->tfm = lmk->hash_tfm;
+	desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
 
-	r = crypto_shash_init(&sdesc.desc);
+	r = crypto_shash_init(desc);
 	if (r)
 		return r;
 
 	if (lmk->seed) {
-		r = crypto_shash_update(&sdesc.desc, lmk->seed, LMK_SEED_SIZE);
+		r = crypto_shash_update(desc, lmk->seed, LMK_SEED_SIZE);
 		if (r)
 			return r;
 	}
 
 	/* Sector is always 512B, block size 16, add data of blocks 1-31 */
-	r = crypto_shash_update(&sdesc.desc, data + 16, 16 * 31);
+	r = crypto_shash_update(desc, data + 16, 16 * 31);
 	if (r)
 		return r;
 
@@ -557,12 +556,12 @@ static int crypt_iv_lmk_one(struct crypt_config *cc, u8 *iv,
 	buf[1] = cpu_to_le32((((u64)dmreq->iv_sector >> 32) & 0x00FFFFFF) | 0x80000000);
 	buf[2] = cpu_to_le32(4024);
 	buf[3] = 0;
-	r = crypto_shash_update(&sdesc.desc, (u8 *)buf, sizeof(buf));
+	r = crypto_shash_update(desc, (u8 *)buf, sizeof(buf));
 	if (r)
 		return r;
 
 	/* No MD5 padding here */
-	r = crypto_shash_export(&sdesc.desc, &md5state);
+	r = crypto_shash_export(desc, &md5state);
 	if (r)
 		return r;
 
@@ -679,10 +678,9 @@ static int crypt_iv_tcw_whitening(struct crypt_config *cc,
 	struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
 	u64 sector = cpu_to_le64((u64)dmreq->iv_sector);
 	u8 buf[TCW_WHITENING_SIZE];
-	struct {
-		struct shash_desc desc;
-		char ctx[crypto_shash_descsize(tcw->crc32_tfm)];
-	} sdesc;
+	char sdesc[sizeof(struct shash_desc)
+		+ crypto_shash_descsize(tcw->crc32_tfm)] CRYPTO_MINALIGN_ATTR;
+	struct shash_desc *desc = (struct shash_desc *)sdesc;
 	int i, r;
 
 	/* xor whitening with sector number */
@@ -691,16 +689,16 @@ static int crypt_iv_tcw_whitening(struct crypt_config *cc,
 	crypto_xor(&buf[8], (u8 *)&sector, 8);
 
 	/* calculate crc32 for every 32bit part and xor it */
-	sdesc.desc.tfm = tcw->crc32_tfm;
-	sdesc.desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP;
+	desc->tfm = tcw->crc32_tfm;
+	desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
 	for (i = 0; i < 4; i++) {
-		r = crypto_shash_init(&sdesc.desc);
+		r = crypto_shash_init(desc);
 		if (r)
 			goto out;
-		r = crypto_shash_update(&sdesc.desc, &buf[i * 4], 4);
+		r = crypto_shash_update(desc, &buf[i * 4], 4);
 		if (r)
 			goto out;
-		r = crypto_shash_final(&sdesc.desc, &buf[i * 4]);
+		r = crypto_shash_final(desc, &buf[i * 4]);
 		if (r)
 			goto out;
 	}
Signature · 6 lines
1.9.1

--
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#2 lore raw

RFC PATCH 2/6 crypto: LLVMLinux: Remove VLAIS usage from crypto/hmac.c

In reply to [email protected]

From: Jan-Simon Möller <[email protected]>

The use of variable length arrays in structs (VLAIS) in the Linux Kernel code precludes the use of compilers which don't implement VLAIS (for instance the Clang compiler). This patch instead allocates the appropriate amount of memory using an char array.

struct shash_desc contains a flexible array member member ctx declared with CRYPTO_MINALIGN_ATTR, so sizeof(struct shash_desc) aligns the beginning of the array declared after struct shash_desc with long long.

No trailing padding is required because it is not a struct type that can be used in an array.

The CRYPTO_MINALIGN_ATTR is required so that desc is aligned with long long as would be the case for a struct containing a member with CRYPTO_MINALIGN_ATTR.

---
 crypto/hmac.c | 27 +++++++++++++--------------
 1 file changed, 13 insertions(+), 14 deletions(-)
crypto/hmac.c +13 -14
index 8d9544c..00ce204 100644
@@ -52,20 +52,19 @@ static int hmac_setkey(struct crypto_shash *parent,
 	struct hmac_ctx *ctx = align_ptr(opad + ss,
 					 crypto_tfm_ctx_alignment());
 	struct crypto_shash *hash = ctx->hash;
-	struct {
-		struct shash_desc shash;
-		char ctx[crypto_shash_descsize(hash)];
-	} desc;
+	char desc[sizeof(struct shash_desc)
+		+ crypto_shash_descsize(hash)] CRYPTO_MINALIGN_ATTR;
+	struct shash_desc *shash = (struct shash_desc *)desc;
 	unsigned int i;
 
-	desc.shash.tfm = hash;
-	desc.shash.flags = crypto_shash_get_flags(parent) &
-			    CRYPTO_TFM_REQ_MAY_SLEEP;
+	shash->tfm = hash;
+	shash->flags = crypto_shash_get_flags(parent)
+		& CRYPTO_TFM_REQ_MAY_SLEEP;
 
 	if (keylen > bs) {
 		int err;
 
-		err = crypto_shash_digest(&desc.shash, inkey, keylen, ipad);
+		err = crypto_shash_digest(shash, inkey, keylen, ipad);
 		if (err)
 			return err;
 
@@ -81,12 +80,12 @@ static int hmac_setkey(struct crypto_shash *parent,
 		opad[i] ^= 0x5c;
 	}
 
-	return crypto_shash_init(&desc.shash) ?:
-	       crypto_shash_update(&desc.shash, ipad, bs) ?:
-	       crypto_shash_export(&desc.shash, ipad) ?:
-	       crypto_shash_init(&desc.shash) ?:
-	       crypto_shash_update(&desc.shash, opad, bs) ?:
-	       crypto_shash_export(&desc.shash, opad);
+	return crypto_shash_init(shash) ?:
+	       crypto_shash_update(shash, ipad, bs) ?:
+	       crypto_shash_export(shash, ipad) ?:
+	       crypto_shash_init(shash) ?:
+	       crypto_shash_update(shash, opad, bs) ?:
+	       crypto_shash_export(shash, opad);
 }
 
 static int hmac_export(struct shash_desc *pdesc, void *out)
Signature · 6 lines
1.9.1

--
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#3 lore raw

RFC PATCH 3/6 crypto: LLVMLinux: Remove VLAIS usage from libcrc32c.c

In reply to [email protected]

From: Jan-Simon Möller <[email protected]>

The use of variable length arrays in structs (VLAIS) in the Linux Kernel code precludes the use of compilers which don't implement VLAIS (for instance the Clang compiler). This patch instead allocates the appropriate amount of memory using an char array.

struct shash_desc contains a flexible array member member ctx declared with CRYPTO_MINALIGN_ATTR, so sizeof(struct shash_desc) aligns the beginning of the array declared after struct shash_desc with long long.

No trailing padding is required because it is not a struct type that can be used in an array.

The CRYPTO_MINALIGN_ATTR is required so that desc is aligned with long long as would be the case for a struct containing a member with CRYPTO_MINALIGN_ATTR.

---
 lib/libcrc32c.c | 18 +++++++++---------
 1 file changed, 9 insertions(+), 9 deletions(-)
lib/libcrc32c.c +9 -9
index b3131f5..03f2cf3 100644
@@ -41,20 +41,20 @@ static struct crypto_shash *tfm;
 
 u32 crc32c(u32 crc, const void *address, unsigned int length)
 {
-	struct {
-		struct shash_desc shash;
-		char ctx[crypto_shash_descsize(tfm)];
-	} desc;
+	char desc[sizeof(struct shash_desc)
+		+ crypto_shash_descsize(tfm)] CRYPTO_MINALIGN_ATTR;
+	struct shash_desc *shash = (struct shash_desc *)desc;
+	u32 *ctx = (u32 *)shash_desc_ctx(shash);
 	int err;
 
-	desc.shash.tfm = tfm;
-	desc.shash.flags = 0;
-	*(u32 *)desc.ctx = crc;
+	shash->tfm = tfm;
+	shash->flags = 0;
+	*ctx = crc;
 
-	err = crypto_shash_update(&desc.shash, address, length);
+	err = crypto_shash_update(shash, address, length);
 	BUG_ON(err);
 
-	return *(u32 *)desc.ctx;
+	return *ctx;
 }
 
 EXPORT_SYMBOL(crc32c);
Signature · 1 line
1.9.1
#4 lore raw

RFC PATCH 4/6 crypto: LLVMLinux: Remove VLAIS usage from crypto/testmgr.c

In reply to [email protected]

From: Jan-Simon Möller <[email protected]>

The use of variable length arrays in structs (VLAIS) in the Linux Kernel code precludes the use of compilers which don't implement VLAIS (for instance the Clang compiler). This patch instead allocates the appropriate amount of memory using an char array.

struct shash_desc contains a flexible array member member ctx declared with CRYPTO_MINALIGN_ATTR, so sizeof(struct shash_desc) aligns the beginning of the array declared after struct shash_desc with long long.

No trailing padding is required because it is not a struct type that can be used in an array.

The CRYPTO_MINALIGN_ATTR is required so that desc is aligned with long long as would be the case for a struct containing a member with CRYPTO_MINALIGN_ATTR.

---
 crypto/testmgr.c | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)
crypto/testmgr.c +8 -8
index ac2b631..f4bf5c2 100644
@@ -1714,16 +1714,16 @@ static int alg_test_crc32c(const struct alg_test_desc *desc,
 	}
 
 	do {
-		struct {
-			struct shash_desc shash;
-			char ctx[crypto_shash_descsize(tfm)];
-		} sdesc;
+		char sdesc[sizeof(struct shash_desc)
+			+ crypto_shash_descsize(tfm)] CRYPTO_MINALIGN_ATTR;
+		struct shash_desc *shash = (struct shash_desc *)sdesc;
+		u32 *ctx = (u32 *)shash_desc_ctx(shash);
 
-		sdesc.shash.tfm = tfm;
-		sdesc.shash.flags = 0;
+		shash->tfm = tfm;
+		shash->flags = 0;
 
-		*(u32 *)sdesc.ctx = le32_to_cpu(420553207);
-		err = crypto_shash_final(&sdesc.shash, (u8 *)&val);
+		*ctx = le32_to_cpu(420553207);
+		err = crypto_shash_final(shash, (u8 *)&val);
 		if (err) {
 			printk(KERN_ERR "alg: crc32c: Operation failed for "
 			       "%s: %d\n", driver, err);
Signature · 6 lines
1.9.1

--
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#5 lore raw

RFC PATCH 5/6 apparmor: LLVMLinux: Remove VLAIS

In reply to [email protected]

From: Vinícius Tinti <[email protected]>

Replaced the use of a Variable Length Array In Struct (VLAIS) with a C99 compliant equivalent. This is the original VLAIS struct.

struct {
	struct shash_desc shash;
	char ctx[crypto_shash_descsize(apparmor_tfm)];
} desc;

This patch instead allocates the appropriate amount of memory using an char array.

The new code can be compiled with both gcc and clang.

struct shash_desc contains a flexible array member member ctx declared with CRYPTO_MINALIGN_ATTR, so sizeof(struct shash_desc) aligns the beginning of the array declared after struct shash_desc with long long.

No trailing padding is required because it is not a struct type that can be used in an array.

The CRYPTO_MINALIGN_ATTR is required so that desc is aligned with long long as would be the case for a struct containing a member with CRYPTO_MINALIGN_ATTR.

---
 security/apparmor/crypto.c | 19 +++++++++----------
 1 file changed, 9 insertions(+), 10 deletions(-)
security/apparmor/crypto.c +9 -10
index 532471d..62b32e7 100644
@@ -32,10 +32,9 @@ unsigned int aa_hash_size(void)
 int aa_calc_profile_hash(struct aa_profile *profile, u32 version, void *start,
 			 size_t len)
 {
-	struct {
-		struct shash_desc shash;
-		char ctx[crypto_shash_descsize(apparmor_tfm)];
-	} desc;
+	char desc[sizeof(struct shash_desc)
+		+ crypto_shash_descsize(apparmor_tfm)] CRYPTO_MINALIGN_ATTR;
+	struct shash_desc *shash = (struct shash_desc *)desc;
 	int error = -ENOMEM;
 	u32 le32_version = cpu_to_le32(version);
 
@@ -46,19 +45,19 @@ int aa_calc_profile_hash(struct aa_profile *profile, u32 version, void *start,
 	if (!profile->hash)
 		goto fail;
 
-	desc.shash.tfm = apparmor_tfm;
-	desc.shash.flags = 0;
+	shash->tfm = apparmor_tfm;
+	shash->flags = 0;
 
-	error = crypto_shash_init(&desc.shash);
+	error = crypto_shash_init(shash);
 	if (error)
 		goto fail;
-	error = crypto_shash_update(&desc.shash, (u8 *) &le32_version, 4);
+	error = crypto_shash_update(shash, (u8 *) &le32_version, 4);
 	if (error)
 		goto fail;
-	error = crypto_shash_update(&desc.shash, (u8 *) start, len);
+	error = crypto_shash_update(shash, (u8 *) start, len);
 	if (error)
 		goto fail;
-	error = crypto_shash_final(&desc.shash, profile->hash);
+	error = crypto_shash_final(shash, profile->hash);
 	if (error)
 		goto fail;
 
Signature · 6 lines
1.9.1

--
To unsubscribe from this list: send the line "unsubscribe linux-security-module" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
maintainer Acked-by on v1 5/6
#6 lore raw

Re: [PATCH RFC 5/6] apparmor: LLVMLinux: Remove VLAIS

Quoted 22 lines · On 09/02/2014 03:32 PM, [email protected] wrote:

From: Vinícius Tinti <[email protected]>

Replaced the use of a Variable Length Array In Struct (VLAIS) with a C99 compliant equivalent. This is the original VLAIS struct.

struct {
	struct shash_desc shash;
	char ctx[crypto_shash_descsize(apparmor_tfm)];
} desc;

This patch instead allocates the appropriate amount of memory using an char array.

The new code can be compiled with both gcc and clang.

struct shash_desc contains a flexible array member member ctx declared with CRYPTO_MINALIGN_ATTR, so sizeof(struct shash_desc) aligns the beginning of the array declared after struct shash_desc with long long.

No trailing padding is required because it is not a struct type that can be used in an array.

Quoted 8 lines

The CRYPTO_MINALIGN_ATTR is required so that desc is aligned with long long as would be the case for a struct containing a member with CRYPTO_MINALIGN_ATTR.

I'm fine with this, do you want me to pull it into my tree for our next push or do you want this all to go together as a set?

Quoted 49 lines
---
 security/apparmor/crypto.c | 19 +++++++++----------
 1 file changed, 9 insertions(+), 10 deletions(-)
security/apparmor/crypto.c +9 -10
index 532471d..62b32e7 100644
@@ -32,10 +32,9 @@ unsigned int aa_hash_size(void)
 int aa_calc_profile_hash(struct aa_profile *profile, u32 version, void *start,
 			 size_t len)
 {
-	struct {
-		struct shash_desc shash;
-		char ctx[crypto_shash_descsize(apparmor_tfm)];
-	} desc;
+	char desc[sizeof(struct shash_desc)
+		+ crypto_shash_descsize(apparmor_tfm)] CRYPTO_MINALIGN_ATTR;
+	struct shash_desc *shash = (struct shash_desc *)desc;
 	int error = -ENOMEM;
 	u32 le32_version = cpu_to_le32(version);
 
@@ -46,19 +45,19 @@ int aa_calc_profile_hash(struct aa_profile *profile, u32 version, void *start,
 	if (!profile->hash)
 		goto fail;
 
-	desc.shash.tfm = apparmor_tfm;
-	desc.shash.flags = 0;
+	shash->tfm = apparmor_tfm;
+	shash->flags = 0;
 
-	error = crypto_shash_init(&desc.shash);
+	error = crypto_shash_init(shash);
 	if (error)
 		goto fail;
-	error = crypto_shash_update(&desc.shash, (u8 *) &le32_version, 4);
+	error = crypto_shash_update(shash, (u8 *) &le32_version, 4);
 	if (error)
 		goto fail;
-	error = crypto_shash_update(&desc.shash, (u8 *) start, len);
+	error = crypto_shash_update(shash, (u8 *) start, len);
 	if (error)
 		goto fail;
-	error = crypto_shash_final(&desc.shash, profile->hash);
+	error = crypto_shash_final(shash, profile->hash);
 	if (error)
 		goto fail;
 
#7 lore raw

Re: [PATCH RFC 5/6] apparmor: LLVMLinux: Remove VLAIS

Quoted 5 lines · John Johansen

I'm fine with this, do you want me to pull it into my tree for our next push or do you want this all to go together as a set?

I'm more than happy for individual maintainers to pull relevant patches into their trees for the next merge window.

They were presented as a RFC patch set purely to get consensus as a group since its the same change across the board.

Happy to push your patch to you now.

Behan

Signature · 2 lines
Behan Webster
[email protected]
#8 lore raw

PATCH v2 1/1 apparmor: LLVMLinux: Remove VLAIS

In reply to John Johansen

From: Vinícius Tinti <[email protected]>

Replaced the use of a Variable Length Array In Struct (VLAIS) with a C99 compliant equivalent. This is the original VLAIS struct.

struct {
	struct shash_desc shash;
	char ctx[crypto_shash_descsize(apparmor_tfm)];
} desc;

This patch instead allocates the appropriate amount of memory using an char array.

The new code can be compiled with both gcc and clang.

struct shash_desc contains a flexible array member member ctx declared with CRYPTO_MINALIGN_ATTR, so sizeof(struct shash_desc) aligns the beginning of the array declared after struct shash_desc with long long.

No trailing padding is required because it is not a struct type that can be used in an array.

The CRYPTO_MINALIGN_ATTR is required so that desc is aligned with long long as would be the case for a struct containing a member with CRYPTO_MINALIGN_ATTR.

---
 security/apparmor/crypto.c | 19 +++++++++----------
 1 file changed, 9 insertions(+), 10 deletions(-)
security/apparmor/crypto.c +9 -10
index 532471d..62b32e7 100644
@@ -32,10 +32,9 @@ unsigned int aa_hash_size(void)
 int aa_calc_profile_hash(struct aa_profile *profile, u32 version, void *start,
 			 size_t len)
 {
-	struct {
-		struct shash_desc shash;
-		char ctx[crypto_shash_descsize(apparmor_tfm)];
-	} desc;
+	char desc[sizeof(struct shash_desc)
+		+ crypto_shash_descsize(apparmor_tfm)] CRYPTO_MINALIGN_ATTR;
+	struct shash_desc *shash = (struct shash_desc *)desc;
 	int error = -ENOMEM;
 	u32 le32_version = cpu_to_le32(version);
 
@@ -46,19 +45,19 @@ int aa_calc_profile_hash(struct aa_profile *profile, u32 version, void *start,
 	if (!profile->hash)
 		goto fail;
 
-	desc.shash.tfm = apparmor_tfm;
-	desc.shash.flags = 0;
+	shash->tfm = apparmor_tfm;
+	shash->flags = 0;
 
-	error = crypto_shash_init(&desc.shash);
+	error = crypto_shash_init(shash);
 	if (error)
 		goto fail;
-	error = crypto_shash_update(&desc.shash, (u8 *) &le32_version, 4);
+	error = crypto_shash_update(shash, (u8 *) &le32_version, 4);
 	if (error)
 		goto fail;
-	error = crypto_shash_update(&desc.shash, (u8 *) start, len);
+	error = crypto_shash_update(shash, (u8 *) start, len);
 	if (error)
 		goto fail;
-	error = crypto_shash_final(&desc.shash, profile->hash);
+	error = crypto_shash_final(shash, profile->hash);
 	if (error)
 		goto fail;
 
Signature · 1 line
1.9.1
#9 lore raw

RFC PATCH 6/6 btrfs: LLVMLinux: Remove VLAIS

In reply to [email protected]

From: Vinícius Tinti <[email protected]>

Replaced the use of a Variable Length Array In Struct (VLAIS) with a C99 compliant equivalent. This is the original VLAIS struct.

struct {
	struct shash_desc shash;
	char ctx[crypto_shash_descsize(tfm)];
} desc;

This patch instead allocates the appropriate amount of memory using an char array.

The new code can be compiled with both gcc and clang.

struct shash_desc contains a flexible array member member ctx declared with CRYPTO_MINALIGN_ATTR, so sizeof(struct shash_desc) aligns the beginning of the array declared after struct shash_desc with long long.

No trailing padding is required because it is not a struct type that can be used in an array.

The CRYPTO_MINALIGN_ATTR is required so that desc is aligned with long long as would be the case for a struct containing a member with CRYPTO_MINALIGN_ATTR.

---
 fs/btrfs/hash.c | 18 +++++++++---------
 1 file changed, 9 insertions(+), 9 deletions(-)
fs/btrfs/hash.c +9 -9
index 85889aa..2fdacda 100644
@@ -33,18 +33,18 @@ void btrfs_hash_exit(void)
 
 u32 btrfs_crc32c(u32 crc, const void *address, unsigned int length)
 {
-	struct {
-		struct shash_desc shash;
-		char ctx[crypto_shash_descsize(tfm)];
-	} desc;
+	char desc[sizeof(struct shash_desc)
+		+ crypto_shash_descsize(tfm)] CRYPTO_MINALIGN_ATTR;
+	struct shash_desc *shash = (struct shash_desc *)desc;
+	u32 *ctx = (u32 *)shash_desc_ctx(shash);
 	int err;
 
-	desc.shash.tfm = tfm;
-	desc.shash.flags = 0;
-	*(u32 *)desc.ctx = crc;
+	shash->tfm = tfm;
+	shash->flags = 0;
+	*ctx = crc;
 
-	err = crypto_shash_update(&desc.shash, address, length);
+	err = crypto_shash_update(shash, address, length);
 	BUG_ON(err);
 
-	return *(u32 *)desc.ctx;
+	return *ctx;
 }
Signature · 6 lines
1.9.1

--
To unsubscribe from this list: send the line "unsubscribe linux-security-module" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#10 lore raw
In reply to [email protected]

Hi Behan,

Quoted 19 lines

These patches remove the use of Variable Length Arrays In Structs (VLAIS) in crypto related code. Presented here for comments as a whole (since they all do the same thing in the same way). Once everyone is happy I will submit them individually to their appropriate maintainers.

The LLVMLinux project aims to fully build the Linux kernel using both gcc and clang (the C front end for the LLVM compiler infrastructure project).

Jan-Simon Möller (4):
crypto, dm: LLVMLinux: Remove VLAIS usage from dm-crypt
crypto: LLVMLinux: Remove VLAIS usage from crypto/hmac.c
crypto: LLVMLinux: Remove VLAIS usage from libcrc32c.c
crypto: LLVMLinux: Remove VLAIS usage from crypto/testmgr.c

Vinícius Tinti (2):
apparmor: LLVMLinux: Remove VLAIS
btrfs: LLVMLinux: Remove VLAIS

Quoted 7 lines
crypto/hmac.c              | 27 +++++++++++++--------------
crypto/testmgr.c           | 16 ++++++++--------
drivers/md/dm-crypt.c      | 38 ++++++++++++++++++--------------------
fs/btrfs/hash.c            | 18 +++++++++---------
lib/libcrc32c.c            | 18 +++++++++---------
security/apparmor/crypto.c | 19 +++++++++----------
6 files changed, 66 insertions(+), 70 deletions(-)

are you sure these are all of them? I know for a fact that we are using the same construct in net/bluetooth/amp.c as well.

Regards

Marcel

#12 lore raw
In reply to Marcel Holtmann
Quoted 21 lines · Marcel Holtmann

Hi Behan,

Quoted 19 lines

These patches remove the use of Variable Length Arrays In Structs (VLAIS) in crypto related code. Presented here for comments as a whole (since they all do the same thing in the same way). Once everyone is happy I will submit them individually to their appropriate maintainers.

The LLVMLinux project aims to fully build the Linux kernel using both gcc and clang (the C front end for the LLVM compiler infrastructure project).

Jan-Simon Möller (4):
crypto, dm: LLVMLinux: Remove VLAIS usage from dm-crypt
crypto: LLVMLinux: Remove VLAIS usage from crypto/hmac.c
crypto: LLVMLinux: Remove VLAIS usage from libcrc32c.c
crypto: LLVMLinux: Remove VLAIS usage from crypto/testmgr.c

Vinícius Tinti (2):
apparmor: LLVMLinux: Remove VLAIS
btrfs: LLVMLinux: Remove VLAIS

Quoted 8 lines
Quoted 7 lines
crypto/hmac.c              | 27 +++++++++++++--------------
crypto/testmgr.c           | 16 ++++++++--------
drivers/md/dm-crypt.c      | 38 ++++++++++++++++++--------------------
fs/btrfs/hash.c            | 18 +++++++++---------
lib/libcrc32c.c            | 18 +++++++++---------
security/apparmor/crypto.c | 19 +++++++++----------
6 files changed, 66 insertions(+), 70 deletions(-)

are you sure these are all of them? I know for a fact that we are using the same construct in net/bluetooth/amp.c as well.

There have been other places where this was an issue before too (ext4, mac80211, USB gadget, ...). Some have already been fixed.

Hmm. Yeah, I thought we had a patch for bluetooth too. I can't find it now though.

Suffice it to say that similar patches are required for the other instances of this kind of code elsewhere as well.

Thanks Marcel,

Behan

Signature · 2 lines
Behan Webster
[email protected]