Skip to content

Commit

Permalink
lib/zlib: unpoison DFLTCC output buffers
Browse files Browse the repository at this point in the history
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] zlib-ng/zlib-ng@1f5ddcc

Link: https://lkml.kernel.org/r/[email protected]
Signed-off-by: Ilya Leoshkevich <[email protected]>
Reported-by: Alexander Gordeev <[email protected]>
Reviewed-by: Alexander Potapenko <[email protected]>
Cc: Christian Borntraeger <[email protected]>
Cc: Christoph Lameter <[email protected]>
Cc: David Rientjes <[email protected]>
Cc: Dmitry Vyukov <[email protected]>
Cc: Heiko Carstens <[email protected]>
Cc: Hyeonggon Yoo <[email protected]>
Cc: Joonsoo Kim <[email protected]>
Cc: <[email protected]>
Cc: Marco Elver <[email protected]>
Cc: Mark Rutland <[email protected]>
Cc: Masami Hiramatsu (Google) <[email protected]>
Cc: Pekka Enberg <[email protected]>
Cc: Roman Gushchin <[email protected]>
Cc: Steven Rostedt (Google) <[email protected]>
Cc: Sven Schnelle <[email protected]>
Cc: Vasily Gorbik <[email protected]>
Cc: Vlastimil Babka <[email protected]>
Signed-off-by: Andrew Morton <[email protected]>
  • Loading branch information
iii-i authored and akpm00 committed Jul 4, 2024
1 parent 4d7b5a2 commit 89f42df
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/zlib_dfltcc/dfltcc.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ struct dfltcc_param_v0 {
uint8_t csb[1152];
};

static_assert(offsetof(struct dfltcc_param_v0, csb) == 384);
static_assert(sizeof(struct dfltcc_param_v0) == 1536);

#define CVT_CRC32 0
Expand Down
28 changes: 28 additions & 0 deletions lib/zlib_dfltcc/dfltcc_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#ifndef DFLTCC_UTIL_H
#define DFLTCC_UTIL_H

#include "dfltcc.h"
#include <linux/kmsan-checks.h>
#include <linux/zutil.h>

/*
Expand All @@ -20,6 +22,7 @@ typedef enum {
#define DFLTCC_CMPR 2
#define DFLTCC_XPND 4
#define HBT_CIRCULAR (1 << 7)
#define DFLTCC_FN_MASK ((1 << 7) - 1)
#define HB_BITS 15
#define HB_SIZE (1 << HB_BITS)

Expand All @@ -34,6 +37,7 @@ static inline dfltcc_cc dfltcc(
)
{
Byte *t2 = op1 ? *op1 : NULL;
unsigned char *orig_t2 = t2;
size_t t3 = len1 ? *len1 : 0;
const Byte *t4 = op2 ? *op2 : NULL;
size_t t5 = len2 ? *len2 : 0;
Expand All @@ -59,6 +63,30 @@ static inline dfltcc_cc dfltcc(
: "cc", "memory");
t2 = r2; t3 = r3; t4 = r4; t5 = r5;

/*
* Unpoison the parameter block and the output buffer.
* This is a no-op in non-KMSAN builds.
*/
switch (fn & DFLTCC_FN_MASK) {
case DFLTCC_QAF:
kmsan_unpoison_memory(param, sizeof(struct dfltcc_qaf_param));
break;
case DFLTCC_GDHT:
kmsan_unpoison_memory(param, offsetof(struct dfltcc_param_v0, csb));
break;
case DFLTCC_CMPR:
kmsan_unpoison_memory(param, sizeof(struct dfltcc_param_v0));
kmsan_unpoison_memory(
orig_t2,
t2 - orig_t2 +
(((struct dfltcc_param_v0 *)param)->sbb == 0 ? 0 : 1));
break;
case DFLTCC_XPND:
kmsan_unpoison_memory(param, sizeof(struct dfltcc_param_v0));
kmsan_unpoison_memory(orig_t2, t2 - orig_t2);
break;
}

if (op1)
*op1 = t2;
if (len1)
Expand Down

0 comments on commit 89f42df

Please sign in to comment.