Skip to contentSkip to conversation

fscache: fscache_set_key() - align alloc and usage

PATCH In review Tomas Bortoli 2 participants 3 messages Jul 22, 2018 – Aug 3, 2018

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 fscache: fscache_set_key() - align alloc and usage

The fscache_set_key() function allocates the buf pointer if index_key_len > sizeof(cookie->inline_key). In such cases the allocated space might not be aligned with the pointer type.
This may result in an out-of-bound in the for-loop later in the same function, as the counter is rounded up.

---
 fs/fscache/cookie.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
fs/fscache/cookie.c +1 -1
index 97137d7ec5ee..ed28bfb6a0fe 100644
@@ -98,7 +98,7 @@ static int fscache_set_key(struct fscache_cookie *cookie,
 	cookie->key_len = index_key_len;
 
 	if (index_key_len > sizeof(cookie->inline_key)) {
-		buf = kzalloc(index_key_len, GFP_KERNEL);
+		buf = kzalloc(round_up(index_key_len, sizeof(u32)), GFP_KERNEL);
 		if (!buf)
 			return -ENOMEM;
 		cookie->key = buf;
Signature · 1 line
2.11.0
#1 lore raw
Quoted 4 lines · Tomas Bortoli

The fscache_set_key() function allocates the buf pointer if index_key_len > sizeof(cookie->inline_key). In such cases the allocated space might not be aligned with the pointer type. This may result in an out-of-bound in the for-loop later in the same function, as the counter is rounded up.

Yeah, it's good idea anyway since I should tell the allocator everything that I expect to use - though kmalloc() will effectively rounds up the size to a multiple of 8 anyway (ie. the smallest allocation granule is 8 bytes).

David

#2 lore raw
Quoted 7 lines · David Howells
Quoted 4 lines · Tomas Bortoli

The fscache_set_key() function allocates the buf pointer if index_key_len > sizeof(cookie->inline_key). In such cases the allocated space might not be aligned with the pointer type. This may result in an out-of-bound in the for-loop later in the same function, as the counter is rounded up.

Quoted 6 lines

Yeah, it's good idea anyway since I should tell the allocator everything that I expect to use - though kmalloc() will effectively rounds up the size to a multiple of 8 anyway (ie. the smallest allocation granule is 8 bytes).

David

Yeah I forgot that :) at least KASAN won't complain anymore.

Tomas