Line data Source code
1 : /*
2 : SSSD
3 :
4 : Authors:
5 : Simo Sorce <ssorce@redhat.com>
6 :
7 : Copyright (C) Red Hat, Inc 2007
8 :
9 : This program is free software; you can redistribute it and/or modify
10 : it under the terms of the GNU Lesser General Public License as published by
11 : the Free Software Foundation; either version 3 of the License, or
12 : (at your option) any later version.
13 :
14 : This program is distributed in the hope that it will be useful,
15 : but WITHOUT ANY WARRANTY; without even the implied warranty of
16 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 : GNU Lesser General Public License for more details.
18 :
19 : You should have received a copy of the GNU Lesser General Public License
20 : along with this program. If not, see <http://www.gnu.org/licenses/>.
21 : */
22 :
23 : /* CAUTION:
24 : * This file is also used in sss_client (pam, nss). Therefore it has to be
25 : * minimalist and cannot include DEBUG macros or header file util.h.
26 : */
27 :
28 :
29 : #ifndef _UTIL_SAFEALIGN_H
30 : #define _UTIL_SAFEALIGN_H
31 :
32 : #include <string.h>
33 : #include <stdint.h>
34 :
35 : /* Use this macro to suppress alignment warnings (use it
36 : * only to suppress false-positives) */
37 : #define DISCARD_ALIGN(ptr, type) ((type)(void *)(ptr))
38 :
39 : #define IS_ALIGNED(ptr, type) \
40 : ((uintptr_t)(ptr) % sizeof(type) == 0)
41 :
42 : #define PADDING_SIZE(base, type) \
43 : ((sizeof(type) - ((base) % sizeof(type))) % sizeof(type))
44 :
45 : #define SIZE_T_OVERFLOW(current, add) \
46 : (((size_t)(add)) > (SIZE_MAX - ((size_t)(current))))
47 :
48 : static inline void
49 2150 : safealign_memcpy(void *dest, const void *src, size_t n, size_t *counter)
50 : {
51 2150 : memcpy(dest, src, n);
52 2150 : if (counter) {
53 1210 : *counter += n;
54 : }
55 2150 : }
56 :
57 : #define SAFEALIGN_SETMEM_VALUE(dest, value, type, pctr) do { \
58 : type CV_MACRO_val = (type)(value); \
59 : safealign_memcpy(dest, &CV_MACRO_val, sizeof(type), pctr); \
60 : } while(0)
61 :
62 : /* SAFEALIGN_COPY_INT64(void *dest, void *src, size_t *pctr)
63 : * This macro will safely copy sizeof(int64_t) bytes from memory
64 : * location pointed by 'src' to memory location pointed by 'dest'.
65 : * If the 'pctr' pointer is not NULL, the value it points to will
66 : * be incremented by sizeof(int64_t). */
67 : #define SAFEALIGN_COPY_INT64(dest, src, pctr) \
68 : safealign_memcpy(dest, src, sizeof(int64_t), pctr)
69 :
70 : /* SAFEALIGN_SETMEM_INT64(void *dest, int64_t value, size_t *pctr)
71 : * This macro will safely assign an int64_t value to the memory
72 : * location pointed by 'dest'. If the 'pctr' pointer is not NULL,
73 : * the value it points to will be incremented by sizeof(int64_t). */
74 : #define SAFEALIGN_SETMEM_INT64(dest, value, pctr) \
75 : SAFEALIGN_SETMEM_VALUE(dest, value, int64_t, pctr)
76 :
77 : /* SAFEALIGN_COPY_UINT32(void *dest, void *src, size_t *pctr) */
78 : #define SAFEALIGN_COPY_UINT32(dest, src, pctr) \
79 : safealign_memcpy(dest, src, sizeof(uint32_t), pctr)
80 :
81 : /* SAFEALIGN_SETMEM_UINT32(void *dest, uint32_t value, size_t *pctr) */
82 : #define SAFEALIGN_SETMEM_UINT32(dest, value, pctr) \
83 : SAFEALIGN_SETMEM_VALUE(dest, value, uint32_t, pctr)
84 :
85 : /* SAFEALIGN_COPY_INT32(void *dest, void *src, size_t *pctr) */
86 : #define SAFEALIGN_COPY_INT32(dest, src, pctr) \
87 : safealign_memcpy(dest, src, sizeof(int32_t), pctr)
88 :
89 : /* SAFEALIGN_SETMEM_INT32(void *dest, int32_t value, size_t *pctr) */
90 : #define SAFEALIGN_SETMEM_INT32(dest, value, pctr) \
91 : SAFEALIGN_SETMEM_VALUE(dest, value, int32_t, pctr)
92 :
93 : /* SAFEALIGN_COPY_UINT16(void *dest, void *src, size_t *pctr) */
94 : #define SAFEALIGN_COPY_UINT16(dest, src, pctr) \
95 : safealign_memcpy(dest, src, sizeof(uint16_t), pctr)
96 :
97 : /* SAFEALIGN_SETMEM_UINT16(void *dest, uint16_t value, size_t *pctr) */
98 : #define SAFEALIGN_SETMEM_UINT16(dest, value, pctr) \
99 : SAFEALIGN_SETMEM_VALUE(dest, value, uint16_t, pctr)
100 :
101 : /* These macros are the same as their equivalents without _CHECK suffix,
102 : * but additionally make the caller return EINVAL immediatelly if *pctr
103 : * would excceed len. */
104 : #define SAFEALIGN_COPY_UINT32_CHECK(dest, src, len, pctr) do { \
105 : if ((*(pctr) + sizeof(uint32_t)) > (len) || \
106 : SIZE_T_OVERFLOW(*(pctr), sizeof(uint32_t))) { return EINVAL; } \
107 : safealign_memcpy(dest, src, sizeof(uint32_t), pctr); \
108 : } while(0)
109 :
110 : #define SAFEALIGN_COPY_INT32_CHECK(dest, src, len, pctr) do { \
111 : if ((*(pctr) + sizeof(int32_t)) > (len) || \
112 : SIZE_T_OVERFLOW(*(pctr), sizeof(int32_t))) { return EINVAL; } \
113 : safealign_memcpy(dest, src, sizeof(int32_t), pctr); \
114 : } while(0)
115 :
116 : #define SAFEALIGN_COPY_UINT16_CHECK(dest, src, len, pctr) do { \
117 : if ((*(pctr) + sizeof(uint16_t)) > (len) || \
118 : SIZE_T_OVERFLOW(*(pctr), sizeof(uint16_t))) { return EINVAL; } \
119 : safealign_memcpy(dest, src, sizeof(uint16_t), pctr); \
120 : } while(0)
121 :
122 : /* Aliases for backward compatibility. */
123 : #define SAFEALIGN_SET_VALUE SAFEALIGN_SETMEM_VALUE
124 : #define SAFEALIGN_SET_INT64 SAFEALIGN_SETMEM_INT64
125 : #define SAFEALIGN_SET_UINT32 SAFEALIGN_SETMEM_UINT32
126 : #define SAFEALIGN_SET_INT32 SAFEALIGN_SETMEM_INT32
127 : #define SAFEALIGN_SET_UINT16 SAFEALIGN_SETMEM_UINT16
128 :
129 : #endif /* _UTIL_SAFEALIGN_H */
|