1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
docs / unsafe_buffers.md [blame]
# Preventing OOB through Unsafe Buffers errors (aka Spanification)
Out-of-bounds (OOB) security bugs commonly happen through C-style pointers which
have no bounds information associated with them. We can prevent such
bugs by always using C++ containers. Furthermore, the Clang compiler can
warn about unsafe pointer usage that should be converted to containers.
When an unsafe usage is detected, Clang prints a warning similar to
```
error: unsafe buffer access [-Werror,-Wunsafe-buffer-usage]
```
and directs developers to this file for more information.
Clang documentation includes a guide to working with unsafe-buffer-usage
warnings here: https://clang.llvm.org/docs/SafeBuffers.html
[TOC]
## Suppressions
Our [compiler](../tools/clang/plugins/UnsafeBuffersPlugin.cpp) enables
the `-Wunsafe-buffer-usage` warning on all files by default. Because the
Chromium codebase is not yet compliant with these warnings, there are
mechanisms to opt out code on a directory, file, or per-occurence basis.
Entire directories are opted out of unsafe pointer usage warnings through
the [`//build/config/unsafe_buffers_paths.txt`](../build/config/unsafe_buffers_paths.txt)
file. As work progresses, directories will be removed from this list, and
non-compliant files marked on a per-file basis as below. Early results
indicate that often 85%+ of files in a directory already happen to be
compliant, so file-by-file suppression allows this code to be subject
to enforcement.
Individual files are opted out of unsafe pointer usage warnings though
the use of the following snippet, which is to be placed immediately
following the copyright header in a source file.
```
#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/ABC): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif
```
Individual expressions or blocks of code are opted out by using the
`UNSAFE_BUFFERS()` macro as defined in [`//base/compiler_specific.h`[(../base/compiler_specific.h)
file. These should be rare once a project is fully converted, except
perhaps when working with C-style external APIs. These must
always be accompanied by a `// SAFETY:` comment explaining in detail
how the code has been evaluated to be safe for all possible input.
Code introducing UNSAFE_BUFFERS() macro invocations without corresponding
`// SAFETY:` comment should be summarily rejected during code review.
To allow for incremental conversion, code can be temporarily opted out by
using the `UNSAFE_TODO()` macro. This provides the same functionality as
the `UNSAFE_BUFFERS()` macro, but allows easier searching for code in need
of revision. Add TODO() comment, along the lines of
`// TODO(crbug.com/xxxxxx): resolve safety issues`.
## Container-based ecosystem
Containers may be owning types or view types. The common owning containers that
us contiguous storage are `std::vector`, `std::string`, `base::HeapArray`,
`std::array`. Their common view types are `base::span`, `std::string_view`,
`base::cstring_view`.
Other owning containers include maps, sets, deques, etc. These are not
compatible with `base::span` as they are not contiguous and generally do not
have an associated view type at this time.
We are using `base::span` instead of `std::span` in order to provide a type that
can do more than the standard type. We also have other types and functions to
work with ranges and spans instead of unbounded pointers and iterators.
The common conversions to spans are:
- `base::span<T>` replaces `T* ptr, size_t size`.
- `base::span<T, N>` replaces `T (&ptr)[N]` (a reference to a compile-time-sized
array).
- `base::raw_span<T>` replaces `base::span<T>` (and `T* ptr, size_t size`) for
class fields.
### Span construction
- `base::span()` makes a span, deducing the type and size, from any contiguous
range. It can also take explicit begin/end or data/size pairs.
- `base::to_fixed_extent<N>()` makes a fixed-size span from a dynamic one.
- `base::as_bytes()` and `base::as_chars()` convert a span’s inner type to
`uint8_t` or `char` respectively, making a byte-span or char-span.
- `base::span_from_ref()` and `base::byte_span_from_ref()` make a span, or
byte-span, from a single object.
- `base::as_byte_span()` and `base::as_writable_byte_span()` make a
byte-span from any contiguous range.
#### Padding bytes
Note that if the type contains padding bytes that were not somehow explicitly
initialized, this can create reads of uninitialized memory. Conversion to a
byte-span is most commonly used for spans of primitive types, such as going from
`char` (such as in `std::string`) or `uint32_t` (in a `std::vector`) to
`unit8_t`.
### Dynamic read/write of a span
- `base::SpanReader` reads heterogeneous values from a (typically, byte-) span
in a dynamic manner.
- `base::SpanWriter` writes heterogeneous values into a (typically, byte-) span
in a dynamic manner.
### Values to/from byte spans
In [`//base/numerics/byte_conversions.h`](../base/numerics/byte_conversions.h)
we have conversions between byte-arrays and big/little endian integers or
floats. For example (and there are many other variations):
- `base::U32FromBigEndian` converts from a big-endian byte-span to an unsigned
32-bit integer.
- `base::U32FromLittleEndian` converts from a little-endian byte-span to an
unsigned
- `base::U32ToBigEndian` converts from an integer to a big-endian-encoded
4-byte-array.
- `base::U32ToLittleEndian` converts from an integer to a little-endian-encoded
4-byte-array.
### Heap-allocated arrays
- `base::HeapArray<T>` replaces `std::unique_ptr<T[]>` and places the bounds of
the array inside the `HeapArray` which makes it a bounds-safe range.
### Copying and filling arrays
- `base::span::copy_from(span)` replaces `memcpy` and `memmove`, and verifies
that the source and destination spans have the same size instead of writing
out of bounds. It lowers to the same code as `memmove` when possible.
- Note `std::ranges::copy` is not bounds-safe (though its name sounds like
it should be).
- `std::ranges::fill` replaces `memset` and works with a range so you don't
need explicit bounds.
### String pointers
A common form of pointer is `const char*` which is used (sometimes) to represent
a NUL-terminated string. The standard library gives us two types to replace
`char*`, which allow us to know the bounds of the character array and work with
the string as a range:
- `std::string` owns a NUL-terminated string.
- `std::string_view` is a view of a non-NUL-terminated string.
What’s missing is a view of a string that is guaranteed to be NUL-terminated so
that you can call `.c_str()` to generate a `const char*` suitable for C APIs.
- `base::cstring_view` is a view of a NUL-terminated string. This avoids the
need to construct a `std::string` in order to ensure a terminating NUL is
present. Use this as a view type whenever your code bottoms out in a C API
that needs NUL-terminated string pointer.
### Use of std::array<T>.
The clang plugin is very particular about indexing a C-style array (e.g.
`int arr[100]`) with a variable index. Often these issues can be resolved
by replacing this with `std::array<int, 100> arr`, which provides safe
indexed operations. In particular, new code should prefer to use the
`std::array<T, N>` mechanism.
For arrays where the size is determined by the compiler (e.g.
`int arr[] = { 1, 3, 5 };`), the `std::to_array<T>()` helper function
should be used along with the `auto` keyword:
`auto arr = std::to_array<int>({1, 3, 5});`
## Avoid reinterpret_cast
### Writing to a byte span
A common idiom in older code is to write into a byte array by casting
the array into a pointer to a larger type (such as `uint32_t` or `float`)
and then writing through that pointer. This can result in Undefined Behaviour
and violates the rules of the C++ abstract machine.
Instead, keep the byte array as a `base::span<uint8_t>`, and write to it
directly by chunking it up into pieces of the size you want to write.
Using `take_first()` (good for repeated modifications and loops):
```cc
void write_floats(base::span<uint8_t> out, float f1, float f2) {
// Write `f1` into `out`'s prefix, moving `out` forward.
out.take_first<4>().copy_from(base::byte_span_from_ref(f1));
// Write `f2` into `out`'s new prefix (after `f1`).
out.copy_prefix_from(base::byte_span_from_ref(f2));
}
```
Using `split_at()` (good when there are exactly two pieces):
```cc
void write_floats(base::span<uint8_t> out, float f1, float f2) {
// Split `out` into a prefix to write `f1` into, and a remainder.
auto [write_f1, rem] = out.split_at<4>();
// Write `f1` into the prefix portion, `write_f1`.
write_f1.copy_from(base::byte_span_from_ref(f1));
// Write `f2` into the beginning of the remainder.
rem.copy_prefix_from(base::byte_span_from_ref(f2));
}
```
Using `SpanWriter` and endian-aware `FloatToLittleEndian()` (good when non-fatal
APIs are desired):
```cc
void write_floats(base::span<uint8_t> out, float f1, float f2) {
auto writer = base::SpanWriter(out);
CHECK(writer.Write(base::FloatToLittleEndian(f1)));
CHECK(writer.Write(base::FloatToLittleEndian(f2)));
}
```
Writing big-endian, with `SpanWriter` and endian-aware `U32ToBigEndian()`:
```cc
void write_values(base::span<uint8_t> out, uint32_t i1, uint32_t i2) {
auto writer = base::SpanWriter(out);
CHECK(writer.Write(base::U32ToBigEndian(i1)));
// SpanWriter has a built-in shortcut to do the same thing.
CHECK(writer.WriteU32BigEndian(i2));
// Verify we wrote to the whole output. We can put a size parameter in the
// `out` span to push this check to compile-time when it's a constant.
CHECK_EQ(writer.remaining(), 0u);
}
```
Writing an array to a byte span with `copy_from()`:
```cc
void write_floats(base::span<uint8_t> out, std::vector<const float> floats) {
base::span<const uint8_t> byte_floats = base::as_byte_span(floats);
// Or use copy_from() if you want to CHECK at runtime that all of `out` has
// been written to.
out.copy_prefix_from(byte_floats);
}
```
### Reading from a byte span
Instead of turning a `span<const uint8_t>` into a pointer of a larger type,
which can cause Undefined Behaviour, read values out of the byte span and
convert each one as a value (not as a pointer).
Using `take_first()` and endian-aware conversion `FloatFromLittleEndian`:
```cc
void read_floats(base::span<const uint8_t> in, float& f1, float& f2) {
f1 = base::FloatFromLittleEndian(in.take_first<4>());
f2 = base::FloatFromLittleEndian(in.take_first<4>());
}
```
Using `SpanReader` and endian-aware `U32FromBigEndian()`:
```cc
void read_values(base::span<const uint8_t> in, int& i1, int& i2, int& i3) {
auto reader = base::SpanReader(in);
i1 = base::U32FromBigEndian(*reader.Read<4>());
i2 = base::U32FromBigEndian(*reader.Read<4>());
// SpanReader has a built-in shortcut to do the same thing.
CHECK(reader.ReadU32BigEndian(i3));
// Verify we read the whole input. We can put a size parameter in the `in`
// span to push this check to compile-time when it's a constant.
CHECK_EQ(reader.remaining(), 0u);
}
```
## Patterns for spanification
Most pointer issues ought to be resolved by converting to containers. In
particular, one common conversion is to replace `T*` pointers with
`base::span<T>` in a process known as spanification, since most pointers
are unowned references into an array (or vector). The appropriate
replacement for the pointer is
[`base::span`](../base/containers/span.h).
### Copying arrays (`memcpy`)
You have:
```cc
uint8_t array1[12];
uint8_t array2[16];
uint64_t array3[2];
memcpy(array1, array2 + 8, 4);
memcpy(array1 + 4, array3, 8);
```
Spanified:
```cc
uint8_t array1[12];
uint8_t array2[16];
uint64_t array3[2];
base::span<uint8_t> span1(array1);
span1.take_first<4>().copy_from(base::span(array2).subspan<8, 4>());
span1.copy_from(base::as_byte_span(array3).first<8>());
// Use `split_at()` to ensure `array1` is fully written.
auto [from2, from3] = base::span(array1).split_at<4>();
from2.copy_from(base::span(array2).subspan<8, 4>());
from3.copy_from(base::as_byte_span(array3).first<8>());
```
### Zeroing arrays (`memset`)
`std::ranges::fill` works on any range/container and won't write out of
bounds. Converting arbitrary types into a byte array (through
`base::as_writable_byte_span`) is only valid when the type holds trivial
types such as primitives. Unlike `memset`, a constructed object can be
given as the value to use as in `std::ranges_fill(container, Object())`.
You have:
```cc
uint8_t array1[12];
uint64_t array2[2];
Object array3[4];
memset(array1, 0, 12);
memset(array2, 0, 2 * sizeof(uint64_t));
memset(array3, 0, 4 * sizeof(Object));
```
Spanified:
```cc
uint8_t array1[12];
uint64_t array2[2];
Object array3[4];
std::ranges::fill(array1, 0);
std::ranges::fill(array2, 0);
std::ranges::fill(base::as_writable_byte_span(array3), 0);
```
### Comparing arrays (`memcmp`)
You have:
```cc
uint8_t array1[12] = {};
uint8_t array2[12] = {};
bool eq = memcmp(array1, array2, sizeof(array1)) == 0;
bool less = memcmp(array1, array2, sizeof(array1)) < 0;
// In tests.
for (size_t i = 0; i < sizeof(array1); ++i) {
SCOPED_TRACE(i);
EXPECT_EQ(array1[i], array2[i]);
}
```
Spanified:
```cc
uint8_t array1[12] = {};
uint8_t array2[12] = {};
// If one side is a span, the other will convert to span too.
bool eq = base::span(array1) == array2;
bool less = base::span(array1) < array2;
// In tests.
EXPECT_EQ(base::span(array1), array2);
```
### Copying array into an integer
You have:
```cc
uint8_t array[44] = {};
uint32_t v1;
memcpy(&v1, array, sizeof(v1)); // Front.
uint64_t v2;
memcpy(&v2, array + 6, sizeof(v2)); // Middle.
```
Spanified:
```cc
#include "base/numerics/byte_conversions.h"
...
uint8_t array[44] = {};
uint32_t v1 = base::U32FromLittleEndian(base::span(array).first<4u>()); // Front.
uint64_t v2 = base::U64FromLittleEndian(base::span(array).subspan<6u, 8u>()); // Middle.
```
### Copy an array into an integer via cast
Note: This pattern is prone to more UB than out-of-bounds access. It is UB to
cast pointers if the result is not aligned, so these cases are often buggy or
were only correct due to subtle assumptions on buffer alignment. The spanified
version avoids this pitfalls. It has no alignment requirement.
You have:
```cc
uint8_t array[44] = {};
uint32_t v1 = *reinterpret_cast<const uint32_t*>(array); // Front.
uint64_t v2 = *reinterpret_cast<const uint64_t*>(array + 16); // Middle.
```
Spanified:
```cc
#include "base/numerics/byte_conversions.h"
...
uint8_t array[44] = {};
uint32_t v1 = base::U32FromLittleEndian(base::span(array).first<4u>()); // Front.
uint64_t v2 = base::U64FromLittleEndian(base::span(array).subspan<16u, 8u>()); // Middle.
```
### Making a byte array (`span<uint8_t>`) from a string (or any array/range)
You have:
```cc
std::string str = "hello world";
func_with_const_ptr_size(reinterpret_cast<const uint8_t*>(str.data()), str.size());
func_with_mut_ptr_size(reinterpret_cast<uint8_t*>(str.data()), str.size());
```
Spanified:
```cc
std::string str = "hello world";
base::span<const uint8_t> bytes = base::as_byte_span(str);
func_with_const_ptr_size(bytes.data(), bytes.size());
base::span<uint8_t> mut_bytes = base::as_writable_byte_span(str);
func_with_mut_ptr_size(mut_bytes.data(), mut_bytes.size());
// Replace pointer and size with a span, though.
func_with_const_span(base::as_byte_span(str));
func_with_mut_span(base::as_writable_byte_span(str));
```
### Making a byte array (`span<uint8_t>`) from an object
You have:
```cc
uint8_t array[8];
uint64_t val;
two_byte_arrays(array, reinterpret_cast<const uint8_t*>(&val));
```
Spanified:
```cc
uint8_t array[8];
uint64_t val;
base::span<uint8_t> val_span = base::byte_span_from_ref(val);
two_byte_arrays(array, val_span.data());
// Replace an unbounded pointer a span, though.
two_byte_spans(base::span(array), base::byte_span_from_ref(val));
```
### Avoid std::next() for silencing warnings, use ranges
When we convert `pointer + index` to `std::next(pointer, index)` we silence the
`Wunsafe-buffer-usage` warning by pushing the unsafe pointer arithmetic into
the `std::next()` function in a system header, but we have the same unsafety.
`std::next()` does no additional bounds checking.
Instead of using `std::next()`, rewrite away from using pointers (or iterators)
entirely by using ranges. `span()` allows us to take a subset of a contiguous
range without having to use iterators that we move with arithmetic or
`std::next()`.
Likewise, `std::advance()` can silence the warning but does not add any safety
to the pointer arithmetic and should be avoided.
Instead of using pointer/iterator arithmetic:
```cc
// Unsafe buffers warning on the unchecked arithmetic.
auto it = std::find(vec.begin() + offset, vec.end(), 20);
// No warning... But has the same security risk!
auto it = std::find(std::next(vec.begin(), offset), vec.end(), 20);
```
Use a range, with `span()` providing a view of a subset of the range:
```cc
auto it = std::ranges::find(base::span(vec).subspan(offset), 20);
```
# Functions with array pointer parameters
Functions that receive a pointer argument into an array may read
or write out of bounds of that array if subsequent manual size
calculations are incorrect. Such functions should be avoided if
possible, or marked with the `UNSAFE_BUFFER_USAGE` attribute macro
otherwise. This macro propagates to their callers that they must
be called from inside an `UNSAFE_BUFFERS()` region (along with
a corresponding safety comment explaining how the caller knows
the call will be safe).
The same is true for functions that accept an iterator instead
of a range type. Some examples of each are `memcpy()` and
`std::copy()`.
Again, calling such functions is unsafe and should be avoided.
Replace such functions with an API built on base::span
or other range types which prevents any chance of OOB memory
access. For instance, replace `memcpy()`, `std::copy()` and
`std::ranges::copy()` with `base::span::copy_from()`. And
replace `memset()` with `std::ranges::fill()`.
# Aligned memory
An aligned heap allocation can be constructed into a `base::HeapArray` through
the `base::AlignedUninit<T>(size, alignment)` function in
`//base/memory/aligned_memory.h`. It will allocate space for `size` many `T`
objects aligned to `alignment`, and return a `base::AlignedHeapArray<T>` which
is a `base::HeapArray` with an appropriate deleter. Note that the returned
memory is uninitialized.
```cc
base::AlignedHeapArray<float> array = base::AlignedUninit<float>(size, alignment);
```
Some containers are built on top of buffers of `char`s that are aligned for
some other `T` in order to manage the lifetimes of objects in the buffer
through in-place construction (`std::construct_at`) and destruction. While the
memory is allocated and destroyed as `char*`, it is accessed as `T*`. The
`base::AlignedUninitCharArray<T>(size, alignment)` function in
`//base/memory/aligned_memory.h` handles this by returning both:
- A `base::AlignedHeapArray<char>` that will not call destructors on anything in its
buffer.
- A `base::span<T>` that points to all of the (not-yet-created) objects in the
`AlignedHeapArray`. This span can be used to construct `T` objects in place in the
buffer, and the caller is responsible for destroying them as well.
```cc
auto [a, s] = base::AlignedUninitCharArray<float>(size, alignment);
base::AlignedHeapArray<char> array = std::move(a);
base::span<float> span = s;
```