Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

lib/zlib: unpoison DFLTCC output buffers

The constraints of the DFLTCC inline assembly are not precise: they do not
communicate the size of the output buffers to the compiler, so it cannot
automatically instrument it.

Add the manual kmsan_unpoison_memory() calls for the output buffers. The
logic is the same as in [1].

[1] https://github.com/zlib-ng/zlib-ng/commit/1f5ddcc009ac3511e99fc88736a9e1a6381168c5

Link: https://lkml.kernel.org/r/20240621113706.315500-21-iii@linux.ibm.com
Signed-off-by: Ilya Leoshkevich <iii@linux.ibm.com>
Reported-by: Alexander Gordeev <agordeev@linux.ibm.com>
Reviewed-by: Alexander Potapenko <glider@google.com>
Cc: Christian Borntraeger <borntraeger@linux.ibm.com>
Cc: Christoph Lameter <cl@linux.com>
Cc: David Rientjes <rientjes@google.com>
Cc: Dmitry Vyukov <dvyukov@google.com>
Cc: Heiko Carstens <hca@linux.ibm.com>
Cc: Hyeonggon Yoo <42.hyeyoo@gmail.com>
Cc: Joonsoo Kim <iamjoonsoo.kim@lge.com>
Cc: <kasan-dev@googlegroups.com>
Cc: Marco Elver <elver@google.com>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Masami Hiramatsu (Google) <mhiramat@kernel.org>
Cc: Pekka Enberg <penberg@kernel.org>
Cc: Roman Gushchin <roman.gushchin@linux.dev>
Cc: Steven Rostedt (Google) <rostedt@goodmis.org>
Cc: Sven Schnelle <svens@linux.ibm.com>
Cc: Vasily Gorbik <gor@linux.ibm.com>
Cc: Vlastimil Babka <vbabka@suse.cz>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

authored by

Ilya Leoshkevich and committed by
Andrew Morton
89f42df6 4d7b5a2c

+29
+1
lib/zlib_dfltcc/dfltcc.h
··· 80 80 uint8_t csb[1152]; 81 81 }; 82 82 83 + static_assert(offsetof(struct dfltcc_param_v0, csb) == 384); 83 84 static_assert(sizeof(struct dfltcc_param_v0) == 1536); 84 85 85 86 #define CVT_CRC32 0
+28
lib/zlib_dfltcc/dfltcc_util.h
··· 2 2 #ifndef DFLTCC_UTIL_H 3 3 #define DFLTCC_UTIL_H 4 4 5 + #include "dfltcc.h" 6 + #include <linux/kmsan-checks.h> 5 7 #include <linux/zutil.h> 6 8 7 9 /* ··· 22 20 #define DFLTCC_CMPR 2 23 21 #define DFLTCC_XPND 4 24 22 #define HBT_CIRCULAR (1 << 7) 23 + #define DFLTCC_FN_MASK ((1 << 7) - 1) 25 24 #define HB_BITS 15 26 25 #define HB_SIZE (1 << HB_BITS) 27 26 ··· 37 34 ) 38 35 { 39 36 Byte *t2 = op1 ? *op1 : NULL; 37 + unsigned char *orig_t2 = t2; 40 38 size_t t3 = len1 ? *len1 : 0; 41 39 const Byte *t4 = op2 ? *op2 : NULL; 42 40 size_t t5 = len2 ? *len2 : 0; ··· 62 58 , [hist] "r" (hist) 63 59 : "cc", "memory"); 64 60 t2 = r2; t3 = r3; t4 = r4; t5 = r5; 61 + 62 + /* 63 + * Unpoison the parameter block and the output buffer. 64 + * This is a no-op in non-KMSAN builds. 65 + */ 66 + switch (fn & DFLTCC_FN_MASK) { 67 + case DFLTCC_QAF: 68 + kmsan_unpoison_memory(param, sizeof(struct dfltcc_qaf_param)); 69 + break; 70 + case DFLTCC_GDHT: 71 + kmsan_unpoison_memory(param, offsetof(struct dfltcc_param_v0, csb)); 72 + break; 73 + case DFLTCC_CMPR: 74 + kmsan_unpoison_memory(param, sizeof(struct dfltcc_param_v0)); 75 + kmsan_unpoison_memory( 76 + orig_t2, 77 + t2 - orig_t2 + 78 + (((struct dfltcc_param_v0 *)param)->sbb == 0 ? 0 : 1)); 79 + break; 80 + case DFLTCC_XPND: 81 + kmsan_unpoison_memory(param, sizeof(struct dfltcc_param_v0)); 82 + kmsan_unpoison_memory(orig_t2, t2 - orig_t2); 83 + break; 84 + } 65 85 66 86 if (op1) 67 87 *op1 = t2;