···200200 return p;201201}202202203203-__FORTIFY_INLINE void *memset(void *p, int c, __kernel_size_t size)203203+__FORTIFY_INLINE void fortify_memset_chk(__kernel_size_t size,204204+ const size_t p_size,205205+ const size_t p_size_field)204206{205205- size_t p_size = __builtin_object_size(p, 0);207207+ if (__builtin_constant_p(size)) {208208+ /*209209+ * Length argument is a constant expression, so we210210+ * can perform compile-time bounds checking where211211+ * buffer sizes are known.212212+ */206213207207- if (__builtin_constant_p(size) && p_size < size)208208- __write_overflow();209209- if (p_size < size)210210- fortify_panic(__func__);211211- return __underlying_memset(p, c, size);214214+ /* Error when size is larger than enclosing struct. */215215+ if (p_size > p_size_field && p_size < size)216216+ __write_overflow();217217+218218+ /* Warn when write size is larger than dest field. */219219+ if (p_size_field < size)220220+ __write_overflow_field(p_size_field, size);221221+ }222222+ /*223223+ * At this point, length argument may not be a constant expression,224224+ * so run-time bounds checking can be done where buffer sizes are225225+ * known. (This is not an "else" because the above checks may only226226+ * be compile-time warnings, and we want to still warn for run-time227227+ * overflows.)228228+ */229229+230230+ /*231231+ * Always stop accesses beyond the struct that contains the232232+ * field, when the buffer's remaining size is known.233233+ * (The -1 test is to optimize away checks where the buffer234234+ * lengths are unknown.)235235+ */236236+ if (p_size != (size_t)(-1) && p_size < size)237237+ fortify_panic("memset");212238}239239+240240+#define __fortify_memset_chk(p, c, size, p_size, p_size_field) ({ \241241+ size_t __fortify_size = (size_t)(size); \242242+ fortify_memset_chk(__fortify_size, p_size, p_size_field), \243243+ __underlying_memset(p, c, __fortify_size); \244244+})245245+246246+/*247247+ * __builtin_object_size() must be captured here to avoid evaluating argument248248+ * side-effects further into the macro layers.249249+ */250250+#define memset(p, c, s) __fortify_memset_chk(p, c, s, \251251+ __builtin_object_size(p, 0), __builtin_object_size(p, 1))213252214253/*215254 * To make sure the compiler can enforce protection against buffer overflows,···440401/* Don't use these outside the FORITFY_SOURCE implementation */441402#undef __underlying_memchr442403#undef __underlying_memcmp443443-#undef __underlying_memset444404#undef __underlying_strcat445405#undef __underlying_strcpy446406#undef __underlying_strlen