Skip to content

eBPF-based security platform

overflow: round a quotient up without adding d - 1 first

Daniel Borkmannceec93656bc1
diff --git a/.agents/skills/coding/SKILL.md b/.agents/skills/coding/SKILL.md
index 442df0ba..fb3deb63 100644
--- a/.agents/skills/coding/SKILL.md
+++ b/.agents/skills/coding/SKILL.md
@@ -272,7 +272,7 @@ already, or should. The basics:
| A string's prefix, or a span that must be one literal | `str_has_prefix()` to test a prefix, `str_skip_prefix()` to step past it, `span_equals()` for a span of known length (`<shirudo/string.h>`); never `strncmp()` with a length counted by hand |
| A file name's extension | `name_has_suffix()` (`<shirudo/string.h>`), `upatch_select()` for a scandir of upatches |
| Text from outside on a terminal | `text_sanitize()`; UTF-8 with `utf8_width()` |
-| Sizes and lengths | `check_add_overflow()` and kin, `size_add()`, `size_mul()`, `struct_size()` (`<shirudo/overflow.h>`) |
+| Sizes and lengths | `check_add_overflow()` and kin, `size_add()`, `size_mul()`, `struct_size()`, `DIV_ROUND_UP()` (`<shirudo/overflow.h>`) |
| Smaller or larger | `min()`, `max()` (`<shirudo/misc.h>`) |
| Memory | `xmalloc()`, `xcalloc()`, `xreallocarray()`, `xstrndup()`, `xasprintf()`, `xfree(p, size)` (`util/malloc.h`) |
| Secrets | `memzero_explicit()`, `memneq_explicit()` |
diff --git a/include/shirudo/overflow.h b/include/shirudo/overflow.h
index 252fa6d9..5f9b3ee8 100644
--- a/include/shirudo/overflow.h
+++ b/include/shirudo/overflow.h
@@ -2,7 +2,8 @@
/*
* Copyright (c) 2026 Authors of shirudo
*
- * Integer arithmetic that reports or saturates instead of wrapping.
+ * Integer arithmetic that reports, saturates or rounds instead of
+ * wrapping.
*/
#ifndef SHIRUDO_OVERFLOW_H
@@ -65,6 +66,17 @@ static inline size_t __must_check size_sub(size_t a, size_t b)
return bytes;
}
+/*
+ * n / d rounded up, for n of 0 and up and d of 1 and up, without the
+ * n + d - 1 that wraps when n is near the top of its type.
+ */
+#define DIV_ROUND_UP(n, d) ({ \
+ __auto_type __n = (n); \
+ __auto_type __d = (d); \
+ \
+ __n / __d + (__n % __d != 0); \
+})
+
/*
* The size of *p with count elements in its flexible array member, which
* has to be an array and not a pointer.
diff --git a/testing/unit/overflow.c b/testing/unit/overflow.c
index 5bbf0d29..3c62413f 100644
--- a/testing/unit/overflow.c
+++ b/testing/unit/overflow.c
@@ -59,10 +59,21 @@ static void struct_size_counts_the_flexible_array(void)
EXPECT_UEQ(struct_size(p, tail, SIZE_MAX / 4), SIZE_MAX);
}
+static void div_round_up_never_wraps(void)
+{
+ EXPECT_UEQ(DIV_ROUND_UP(0U, 8U), 0);
+ EXPECT_UEQ(DIV_ROUND_UP(1U, 8U), 1);
+ EXPECT_UEQ(DIV_ROUND_UP(8U, 8U), 1);
+ EXPECT_UEQ(DIV_ROUND_UP(9U, 8U), 2);
+ EXPECT_UEQ(DIV_ROUND_UP(UINT32_MAX, 8U), 0x20000000);
+ EXPECT_UEQ(DIV_ROUND_UP(SIZE_MAX, (size_t)2), SIZE_MAX / 2 + 1);
+}
+
static const struct unit_case cases[] = {
UNIT_CASE(check_overflow_judges_by_the_destination),
UNIT_CASE(sizes_stick_at_size_max),
UNIT_CASE(struct_size_counts_the_flexible_array),
+ UNIT_CASE(div_round_up_never_wraps),
{}
};
diff --git a/util/config_image.c b/util/config_image.c
index 6f1ae1bd..67738587 100644
--- a/util/config_image.c
+++ b/util/config_image.c
@@ -3,6 +3,7 @@
#include <shirudo/byteorder.h>
#include <shirudo/misc.h>
+#include <shirudo/overflow.h>
#include <shirudo/string.h>
#include <errno.h>
@@ -45,8 +46,8 @@ static int verity_hash_blocks(__u64 data_blocks, __u64 *out)
CONFIG_IMAGE_BLOCK_SIZE)
return -EFBIG;
while (data_blocks > 1) {
- data_blocks = (data_blocks + VERITY_DIGESTS_PER_BLOCK - 1) /
- VERITY_DIGESTS_PER_BLOCK;
+ data_blocks = DIV_ROUND_UP(data_blocks,
+ VERITY_DIGESTS_PER_BLOCK);
total += data_blocks;
}
*out = total;
diff --git a/util/crypto_openssl.c b/util/crypto_openssl.c
index 211f6180..a115610b 100644
--- a/util/crypto_openssl.c
+++ b/util/crypto_openssl.c
@@ -28,6 +28,7 @@
#include <openssl/x509_vfy.h>
#include <shirudo/misc.h>
+#include <shirudo/overflow.h>
#include <shirudo/string.h>
#include "crypto.h"
@@ -588,7 +589,7 @@ int base64_decode(const char *in, size_t in_len, __u8 *out,
EVP_ENCODE_CTX *ctx = EVP_ENCODE_CTX_new();
int n = 0, fin = 0, ret = -EINVAL;
- if (!ctx || in_len > INT_MAX || cap < 3 * ((in_len + 3) / 4))
+ if (!ctx || in_len > INT_MAX || cap < 3 * DIV_ROUND_UP(in_len, 4))
goto out;
EVP_DecodeInit(ctx);
if (EVP_DecodeUpdate(ctx, out, &n, (const __u8 *)in,
diff --git a/util/keepalive.c b/util/keepalive.c
index 666cd33a..4a5f08ab 100644
--- a/util/keepalive.c
+++ b/util/keepalive.c
@@ -5,6 +5,7 @@
#include <string.h>
#include <shirudo/misc.h>
+#include <shirudo/overflow.h>
#include <shirudo/string.h>
#include "crypto.h"
@@ -39,7 +40,7 @@ static const char * const cond_order[] = {
int keepalive_mask(const __u8 *codes, size_t n, char *b64, size_t cap)
{
- size_t len = (n + CODES_PER_BYTE - 1) / CODES_PER_BYTE, i;
+ size_t len = DIV_ROUND_UP(n, CODES_PER_BYTE), i;
__u8 raw[KEEPALIVE_MASK_BYTES] = {};
if (n > KEEPALIVE_MASK_OPS)
diff --git a/util/upatch_maps.c b/util/upatch_maps.c
index 09a61c88..c537cc5f 100644
--- a/util/upatch_maps.c
+++ b/util/upatch_maps.c
@@ -343,7 +343,7 @@ static char *btf_format_int(const struct btf_type *t, const void *bytes,
__u32 bits = BTF_INT_BITS(enc_word);
__u32 encoding = BTF_INT_ENCODING(enc_word);
bool is_signed = (encoding & BTF_INT_SIGNED) != 0;
- __u8 bytes_used = (__u8)((bits + 7) / 8);
+ __u8 bytes_used = (__u8)DIV_ROUND_UP(bits, 8);
if (bytes_used == 0 || bytes_used > bytes_size || bytes_used > 8)
return xasprintf("?");
@@ -375,7 +375,7 @@ static char *btf_format_bitfield(const struct btf *btf, __u32 type_id,
const void *bytes, __u32 bytes_size,
__u32 bit_off, __u32 bit_size)
{
- __u32 shift = bit_off % 8, n = (shift + bit_size + 7) / 8, i;
+ __u32 shift = bit_off % 8, n = DIV_ROUND_UP(shift + bit_size, 8), i;
const __u8 *b = (const __u8 *)bytes + bit_off / 8;
const struct btf_type *t;
bool is_signed = false;
diff --git a/util/upatch_rodata.c b/util/upatch_rodata.c
index 50408428..f5c91ee0 100644
--- a/util/upatch_rodata.c
+++ b/util/upatch_rodata.c
@@ -690,7 +690,7 @@ static int json_btf_value(struct json_emit *e, const struct btf *btf,
}
json_key(e, name);
if (bit_size) {
- __u32 n = (bit_off % 8 + bit_size + 7) / 8;
+ __u32 n = DIV_ROUND_UP(bit_off % 8 + bit_size, 8);
__u32 j;
const struct btf_type *member_type =
btf__type_by_id(btf,