XenevaOS
Loading...
Searching...
No Matches
Macros
nospec.h File Reference
#include <linux/kernel.h>
Include dependency graph for nospec.h:

Go to the source code of this file.

Macros

#define array_index_nospec(index, size)
 

Macro Definition Documentation

◆ array_index_nospec

#define array_index_nospec (   index,
  size 
)
Value:
({ \
typeof(index) _i = (index); \
typeof(size) _s = (size); \
/* see header: _i must fit an unsigned long; _s may be size_t */ \
BUILD_BUG_ON(sizeof(_i) > sizeof(unsigned long)); \
unsigned long _mask = \
array_index_mask_nospec((unsigned long)_i, \
(unsigned long)_s); \
(typeof(_i))(_i & _mask); \
})
#define index(str, chr)
Definition string.h:66
size_t size
Definition drm_bridge.h:1299

array_index_nospec - sanitize an array index after a bounds check

For a sequence like:

if (index < size) {
    index = array_index_nospec(index, size);
    val = array[index];
}

if the CPU speculates past the bounds check this clamps the index into [0, size). Used by virtio-rng.c's copy_data() to keep a buggy or malicious virtio-rng backend from steering the memcpy past the end of vi->data.