1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-or-later 2*49cdfc7eSAndroid Build Coastguard Worker /* 3*49cdfc7eSAndroid Build Coastguard Worker * Copyright (c) 2019 Cyril Hrubis <[email protected]> 4*49cdfc7eSAndroid Build Coastguard Worker */ 5*49cdfc7eSAndroid Build Coastguard Worker 6*49cdfc7eSAndroid Build Coastguard Worker /** 7*49cdfc7eSAndroid Build Coastguard Worker * DOC: Guarded buffers introduction 8*49cdfc7eSAndroid Build Coastguard Worker * 9*49cdfc7eSAndroid Build Coastguard Worker * Guarded buffer has a page with PROT_NONE allocated right before the start of 10*49cdfc7eSAndroid Build Coastguard Worker * the buffer and canary after the end of the buffer. That means that any 11*49cdfc7eSAndroid Build Coastguard Worker * memory access before the buffer ends with EFAULT or SEGFAULT and any write 12*49cdfc7eSAndroid Build Coastguard Worker * after the end of the buffer will be detected because it would overwrite the 13*49cdfc7eSAndroid Build Coastguard Worker * canaries. 14*49cdfc7eSAndroid Build Coastguard Worker * 15*49cdfc7eSAndroid Build Coastguard Worker * It should be used for all buffers passed to syscalls to make sure off-by-one 16*49cdfc7eSAndroid Build Coastguard Worker * buffer accesses does not happen. 17*49cdfc7eSAndroid Build Coastguard Worker */ 18*49cdfc7eSAndroid Build Coastguard Worker 19*49cdfc7eSAndroid Build Coastguard Worker #ifndef TST_BUFFERS_H__ 20*49cdfc7eSAndroid Build Coastguard Worker #define TST_BUFFERS_H__ 21*49cdfc7eSAndroid Build Coastguard Worker 22*49cdfc7eSAndroid Build Coastguard Worker /** 23*49cdfc7eSAndroid Build Coastguard Worker * struct tst_buffers - A guarded buffer description for allocator. 24*49cdfc7eSAndroid Build Coastguard Worker * 25*49cdfc7eSAndroid Build Coastguard Worker * Buffer description consist of a pointer to a pointer and buffer type/size 26*49cdfc7eSAndroid Build Coastguard Worker * encoded as a different structure members. 27*49cdfc7eSAndroid Build Coastguard Worker * 28*49cdfc7eSAndroid Build Coastguard Worker * @ptr: A pointer to the pointer to buffer. This is dereferenced and set by the 29*49cdfc7eSAndroid Build Coastguard Worker * allocator. 30*49cdfc7eSAndroid Build Coastguard Worker * @size: A buffer size in bytes. Only one of size and iov_sizes can be set. 31*49cdfc7eSAndroid Build Coastguard Worker * @iov_sizes: An -1 terminated array of sizes used to construct a 32*49cdfc7eSAndroid Build Coastguard Worker * struct iovec buffers. 33*49cdfc7eSAndroid Build Coastguard Worker * @str: If size is zero and iov_sizes is NULL this string is going to be 34*49cdfc7eSAndroid Build Coastguard Worker * copied into the buffer. 35*49cdfc7eSAndroid Build Coastguard Worker */ 36*49cdfc7eSAndroid Build Coastguard Worker struct tst_buffers { 37*49cdfc7eSAndroid Build Coastguard Worker void *ptr; 38*49cdfc7eSAndroid Build Coastguard Worker size_t size; 39*49cdfc7eSAndroid Build Coastguard Worker int *iov_sizes; 40*49cdfc7eSAndroid Build Coastguard Worker char *str; 41*49cdfc7eSAndroid Build Coastguard Worker }; 42*49cdfc7eSAndroid Build Coastguard Worker 43*49cdfc7eSAndroid Build Coastguard Worker /** 44*49cdfc7eSAndroid Build Coastguard Worker * tst_buffers_alloc() - Allocates buffers based on the tst_buffers structure. 45*49cdfc7eSAndroid Build Coastguard Worker * 46*49cdfc7eSAndroid Build Coastguard Worker * @bufs: A NULL terminated array of test buffer descriptions. 47*49cdfc7eSAndroid Build Coastguard Worker * 48*49cdfc7eSAndroid Build Coastguard Worker * This is called from the test library if the tst_test.bufs pointer is set. 49*49cdfc7eSAndroid Build Coastguard Worker */ 50*49cdfc7eSAndroid Build Coastguard Worker void tst_buffers_alloc(struct tst_buffers bufs[]); 51*49cdfc7eSAndroid Build Coastguard Worker 52*49cdfc7eSAndroid Build Coastguard Worker /** 53*49cdfc7eSAndroid Build Coastguard Worker * tst_strdup() - Copies a string into a newly allocated guarded buffer. 54*49cdfc7eSAndroid Build Coastguard Worker * 55*49cdfc7eSAndroid Build Coastguard Worker * @str: A string to be duplicated. 56*49cdfc7eSAndroid Build Coastguard Worker * return: A pointer to the string duplicated in a guarded buffer. 57*49cdfc7eSAndroid Build Coastguard Worker * 58*49cdfc7eSAndroid Build Coastguard Worker * Allocates a buffer with tst_alloc() and copies the string into it. 59*49cdfc7eSAndroid Build Coastguard Worker */ 60*49cdfc7eSAndroid Build Coastguard Worker char *tst_strdup(const char *str); 61*49cdfc7eSAndroid Build Coastguard Worker 62*49cdfc7eSAndroid Build Coastguard Worker /** 63*49cdfc7eSAndroid Build Coastguard Worker * tst_alloc() - Allocates a guarded buffer. 64*49cdfc7eSAndroid Build Coastguard Worker * 65*49cdfc7eSAndroid Build Coastguard Worker * @size: A size of the buffer. 66*49cdfc7eSAndroid Build Coastguard Worker * return: A newly allocated guarded buffer. 67*49cdfc7eSAndroid Build Coastguard Worker */ 68*49cdfc7eSAndroid Build Coastguard Worker void *tst_alloc(size_t size); 69*49cdfc7eSAndroid Build Coastguard Worker 70*49cdfc7eSAndroid Build Coastguard Worker /** 71*49cdfc7eSAndroid Build Coastguard Worker * tst_aprintf() - Printf into a newly allocated guarded buffer. 72*49cdfc7eSAndroid Build Coastguard Worker * 73*49cdfc7eSAndroid Build Coastguard Worker * @fmt: A printf-like format. 74*49cdfc7eSAndroid Build Coastguard Worker * @...: A printf-like parameters. 75*49cdfc7eSAndroid Build Coastguard Worker * return: A newly allocated buffer. 76*49cdfc7eSAndroid Build Coastguard Worker * 77*49cdfc7eSAndroid Build Coastguard Worker * Allocates a buffer with tst_alloc() then prints the data into it. 78*49cdfc7eSAndroid Build Coastguard Worker */ 79*49cdfc7eSAndroid Build Coastguard Worker char *tst_aprintf(const char *fmt, ...) 80*49cdfc7eSAndroid Build Coastguard Worker __attribute__((format (printf, 1, 2))); 81*49cdfc7eSAndroid Build Coastguard Worker 82*49cdfc7eSAndroid Build Coastguard Worker /** 83*49cdfc7eSAndroid Build Coastguard Worker * tst_iovec_alloc() - Allocates a complete iovec structure. 84*49cdfc7eSAndroid Build Coastguard Worker * 85*49cdfc7eSAndroid Build Coastguard Worker * @sizes: A -1 terminated array of buffer sizes. 86*49cdfc7eSAndroid Build Coastguard Worker * return: Newly allocated iovec structure. 87*49cdfc7eSAndroid Build Coastguard Worker */ 88*49cdfc7eSAndroid Build Coastguard Worker struct iovec *tst_iovec_alloc(int sizes[]); 89*49cdfc7eSAndroid Build Coastguard Worker 90*49cdfc7eSAndroid Build Coastguard Worker /** 91*49cdfc7eSAndroid Build Coastguard Worker * tst_free_all() - Frees all allocated buffers. 92*49cdfc7eSAndroid Build Coastguard Worker * 93*49cdfc7eSAndroid Build Coastguard Worker * It's important to free all guarded buffers because the canaries after the 94*49cdfc7eSAndroid Build Coastguard Worker * buffer are checked only when the buffer is being freed. 95*49cdfc7eSAndroid Build Coastguard Worker * 96*49cdfc7eSAndroid Build Coastguard Worker * This is called at the end of the test automatically. 97*49cdfc7eSAndroid Build Coastguard Worker */ 98*49cdfc7eSAndroid Build Coastguard Worker void tst_free_all(void); 99*49cdfc7eSAndroid Build Coastguard Worker 100*49cdfc7eSAndroid Build Coastguard Worker #endif /* TST_BUFFERS_H__ */ 101