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