@@ -106,13 +106,13 @@ extern char *__underlying_strncpy(char *p, const char *q, __kernel_size_t size)
106
106
* Instead, please choose an alternative, so that the expectation
107
107
* of @p's contents is unambiguous:
108
108
*
109
- * +--------------------+-----------------+------------+
110
- * | @p needs to be: | padded to @ size | not padded |
111
- * +====================+=================+============+
112
- * | NUL-terminated | strscpy_pad() | strscpy() |
113
- * +--------------------+-----------------+------------+
114
- * | not NUL-terminated | strtomem_pad() | strtomem() |
115
- * +--------------------+-----------------+------------+
109
+ * +--------------------+-------------------- +------------+
110
+ * | **p** needs to be: | padded to ** size** | not padded |
111
+ * +====================+==================== +============+
112
+ * | NUL-terminated | strscpy_pad() | strscpy() |
113
+ * +--------------------+-------------------- +------------+
114
+ * | not NUL-terminated | strtomem_pad() | strtomem() |
115
+ * +--------------------+-------------------- +------------+
116
116
*
117
117
* Note strscpy*()'s differing return values for detecting truncation,
118
118
* and strtomem*()'s expectation that the destination is marked with
@@ -131,6 +131,21 @@ char *strncpy(char * const POS p, const char *q, __kernel_size_t size)
131
131
return __underlying_strncpy (p , q , size );
132
132
}
133
133
134
+ /**
135
+ * strcat - Append a string to an existing string
136
+ *
137
+ * @p: pointer to NUL-terminated string to append to
138
+ * @q: pointer to NUL-terminated source string to append from
139
+ *
140
+ * Do not use this function. While FORTIFY_SOURCE tries to avoid
141
+ * read and write overflows, this is only possible when the
142
+ * destination buffer size is known to the compiler. Prefer
143
+ * building the string with formatting, via scnprintf() or similar.
144
+ * At the very least, use strncat().
145
+ *
146
+ * Returns @p.
147
+ *
148
+ */
134
149
__FORTIFY_INLINE __diagnose_as (__builtin_strcat , 1 , 2 )
135
150
char * strcat (char * const POS p , const char * q )
136
151
{
@@ -144,6 +159,16 @@ char *strcat(char * const POS p, const char *q)
144
159
}
145
160
146
161
extern __kernel_size_t __real_strnlen (const char * , __kernel_size_t ) __RENAME (strnlen );
162
+ /**
163
+ * strnlen - Return bounded count of characters in a NUL-terminated string
164
+ *
165
+ * @p: pointer to NUL-terminated string to count.
166
+ * @maxlen: maximum number of characters to count.
167
+ *
168
+ * Returns number of characters in @p (NOT including the final NUL), or
169
+ * @maxlen, if no NUL has been found up to there.
170
+ *
171
+ */
147
172
__FORTIFY_INLINE __kernel_size_t strnlen (const char * const POS p , __kernel_size_t maxlen )
148
173
{
149
174
size_t p_size = __member_size (p );
@@ -169,6 +194,19 @@ __FORTIFY_INLINE __kernel_size_t strnlen(const char * const POS p, __kernel_size
169
194
* possible for strlen() to be used on compile-time strings for use in
170
195
* static initializers (i.e. as a constant expression).
171
196
*/
197
+ /**
198
+ * strlen - Return count of characters in a NUL-terminated string
199
+ *
200
+ * @p: pointer to NUL-terminated string to count.
201
+ *
202
+ * Do not use this function unless the string length is known at
203
+ * compile-time. When @p is unterminated, this function may crash
204
+ * or return unexpected counts that could lead to memory content
205
+ * exposures. Prefer strnlen().
206
+ *
207
+ * Returns number of characters in @p (NOT including the final NUL).
208
+ *
209
+ */
172
210
#define strlen (p ) \
173
211
__builtin_choose_expr(__is_constexpr(__builtin_strlen(p)), \
174
212
__builtin_strlen(p), __fortify_strlen(p))
@@ -187,8 +225,26 @@ __kernel_size_t __fortify_strlen(const char * const POS p)
187
225
return ret ;
188
226
}
189
227
190
- /* defined after fortified strlen to reuse it */
228
+ /* Defined after fortified strlen() to reuse it. */
191
229
extern size_t __real_strlcpy (char * , const char * , size_t ) __RENAME (strlcpy );
230
+ /**
231
+ * strlcpy - Copy a string into another string buffer
232
+ *
233
+ * @p: pointer to destination of copy
234
+ * @q: pointer to NUL-terminated source string to copy
235
+ * @size: maximum number of bytes to write at @p
236
+ *
237
+ * If strlen(@q) >= @size, the copy of @q will be truncated at
238
+ * @size - 1 bytes. @p will always be NUL-terminated.
239
+ *
240
+ * Do not use this function. While FORTIFY_SOURCE tries to avoid
241
+ * over-reads when calculating strlen(@q), it is still possible.
242
+ * Prefer strscpy(), though note its different return values for
243
+ * detecting truncation.
244
+ *
245
+ * Returns total number of bytes written to @p, including terminating NUL.
246
+ *
247
+ */
192
248
__FORTIFY_INLINE size_t strlcpy (char * const POS p , const char * const POS q , size_t size )
193
249
{
194
250
size_t p_size = __member_size (p );
@@ -214,8 +270,32 @@ __FORTIFY_INLINE size_t strlcpy(char * const POS p, const char * const POS q, si
214
270
return q_len ;
215
271
}
216
272
217
- /* defined after fortified strnlen to reuse it */
273
+ /* Defined after fortified strnlen() to reuse it. */
218
274
extern ssize_t __real_strscpy (char * , const char * , size_t ) __RENAME (strscpy );
275
+ /**
276
+ * strscpy - Copy a C-string into a sized buffer
277
+ *
278
+ * @p: Where to copy the string to
279
+ * @q: Where to copy the string from
280
+ * @size: Size of destination buffer
281
+ *
282
+ * Copy the source string @p, or as much of it as fits, into the destination
283
+ * @q buffer. The behavior is undefined if the string buffers overlap. The
284
+ * destination @p buffer is always NUL terminated, unless it's zero-sized.
285
+ *
286
+ * Preferred to strlcpy() since the API doesn't require reading memory
287
+ * from the source @q string beyond the specified @size bytes, and since
288
+ * the return value is easier to error-check than strlcpy()'s.
289
+ * In addition, the implementation is robust to the string changing out
290
+ * from underneath it, unlike the current strlcpy() implementation.
291
+ *
292
+ * Preferred to strncpy() since it always returns a valid string, and
293
+ * doesn't unnecessarily force the tail of the destination buffer to be
294
+ * zero padded. If padding is desired please use strscpy_pad().
295
+ *
296
+ * Returns the number of characters copied in @p (not including the
297
+ * trailing %NUL) or -E2BIG if @size is 0 or the copy of @q was truncated.
298
+ */
219
299
__FORTIFY_INLINE ssize_t strscpy (char * const POS p , const char * const POS q , size_t size )
220
300
{
221
301
size_t len ;
@@ -261,7 +341,26 @@ __FORTIFY_INLINE ssize_t strscpy(char * const POS p, const char * const POS q, s
261
341
return __real_strscpy (p , q , len );
262
342
}
263
343
264
- /* defined after fortified strlen and strnlen to reuse them */
344
+ /**
345
+ * strncat - Append a string to an existing string
346
+ *
347
+ * @p: pointer to NUL-terminated string to append to
348
+ * @q: pointer to source string to append from
349
+ * @count: Maximum bytes to read from @q
350
+ *
351
+ * Appends at most @count bytes from @q (stopping at the first
352
+ * NUL byte) after the NUL-terminated string at @p. @p will be
353
+ * NUL-terminated.
354
+ *
355
+ * Do not use this function. While FORTIFY_SOURCE tries to avoid
356
+ * read and write overflows, this is only possible when the sizes
357
+ * of @p and @q are known to the compiler. Prefer building the
358
+ * string with formatting, via scnprintf() or similar.
359
+ *
360
+ * Returns @p.
361
+ *
362
+ */
363
+ /* Defined after fortified strlen() and strnlen() to reuse them. */
265
364
__FORTIFY_INLINE __diagnose_as (__builtin_strncat , 1 , 2 , 3 )
266
365
char * strncat (char * const POS p , const char * const POS q , __kernel_size_t count )
267
366
{
@@ -572,6 +671,20 @@ __FORTIFY_INLINE void *kmemdup(const void * const POS0 p, size_t size, gfp_t gfp
572
671
return __real_kmemdup (p , size , gfp );
573
672
}
574
673
674
+ /**
675
+ * strcpy - Copy a string into another string buffer
676
+ *
677
+ * @p: pointer to destination of copy
678
+ * @q: pointer to NUL-terminated source string to copy
679
+ *
680
+ * Do not use this function. While FORTIFY_SOURCE tries to avoid
681
+ * overflows, this is only possible when the sizes of @q and @p are
682
+ * known to the compiler. Prefer strscpy(), though note its different
683
+ * return values for detecting truncation.
684
+ *
685
+ * Returns @p.
686
+ *
687
+ */
575
688
/* Defined after fortified strlen to reuse it. */
576
689
__FORTIFY_INLINE __diagnose_as (__builtin_strcpy , 1 , 2 )
577
690
char * strcpy (char * const POS p , const char * const POS q )
0 commit comments