Skip to contentSkip to conversation

mmc-utils: Use an unsigned byte counter in DO_IO()

PATCH In review Sebastien Bacher 3 participants 3 messages Oct 8 – Oct 30 MMC, SD and SDIO

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mmc-utils: Use an unsigned byte counter in DO_IO()

From: Sebastien Bacher <[email protected]>

DO_IO() tracks the number of bytes already transferred in a signed ssize_t, which is also used to report errors. When building with _FORTIFY_SOURCE=3 and LTO, gcc 15 can't prove that "nbyte - ret" stays within the destination buffer, and the build fails on the RPMB data read in do_rpmb_write_block():

In function 'read',
inlined from 'do_rpmb_write_block' at mmc_cmds.c:...
bits/unistd.h:32:10: error: '__read_alias' writing 484 or more bytes into a region of size 256 overflows the destination [-Werror=stringop-overflow=]

The loop can't actually overflow, so this is a false positive. Track the progress with a size_t, which can't go negative inside the loop, and only convert to the ssize_t return value at the end. The behaviour is
unchanged: EINTR is retried, other errors return -1, and EOF returns the short count.

This is the default hardening configuration on Ubuntu, so mmc-utils currently fails to build there.

---

---
 mmc_cmds.c | 19 +++++++++++--------
 1 file changed, 11 insertions(+), 8 deletions(-)
mmc_cmds.c +11 -8
index 2d9069f..8e21a25 100644
@@ -2207,18 +2207,21 @@ int do_sanitize(int nargs, char **argv)
 
 #define DO_IO(func, fd, buf, nbyte)					\
 	({												\
-		ssize_t ret = 0, r;							\
+		size_t done = 0;							\
+		ssize_t r;									\
 		do {										\
-			r = func(fd, buf + ret, nbyte - ret);	\
-			if (r < 0 && errno != EINTR) {			\
-				ret = -1;							\
+			r = func(fd, (char *)(buf) + done,		\
+				 (nbyte) - done);					\
+			if (r < 0) {							\
+				if (errno == EINTR)					\
+					continue;						\
+				done = (size_t)-1;					\
 				break;								\
 			}										\
-			else if (r > 0)							\
-				ret += r;							\
-		} while (r != 0 && (size_t)ret < nbyte);	\
+			done += (size_t)r;						\
+		} while (r != 0 && done < (size_t)(nbyte));	\
 													\
-		ret;										\
+		(ssize_t)done;								\
 	})
 
 #define RPMB_MULTI_CMD_MAX_CMDS 3

---
base-commit: c6692d5f9bf1ab6fbf5694a0b61477621cc4d016
change-id: 20261008-do-io-unsigned-counter-568a63baef5c

Best regards,

Signature · 1 line
Sebastien Bacher <[email protected]>
Reviewed-by on v1 1/1
#1 lore raw
Quoted 20 lines

From: Sebastien Bacher <[email protected]>

DO_IO() tracks the number of bytes already transferred in a signed ssize_t, which is also used to report errors. When building with _FORTIFY_SOURCE=3 and LTO, gcc 15 can't prove that "nbyte - ret" stays within the destination buffer, and the build fails on the RPMB data read in do_rpmb_write_block():

In function 'read',
inlined from 'do_rpmb_write_block' at mmc_cmds.c:...
bits/unistd.h:32:10: error: '__read_alias' writing 484 or more bytes into a region of size 256 overflows the destination [-Werror=stringop-overflow=]

The loop can't actually overflow, so this is a false positive. Track the progress with a size_t, which can't go negative inside the loop, and only convert to the ssize_t return value at the end. The behaviour is
unchanged: EINTR is retried, other errors return -1, and EOF returns the short count.

Quoted 6 lines

This is the default hardening configuration on Ubuntu, so mmc-utils currently fails to build there.

---

#2 lore raw
In reply to Sebastien Bacher
Quoted 21 lines · On Thu, Oct 8, 2026 at 11:02 PM Sebastien Bacher via B4 Relay…

From: Sebastien Bacher <[email protected]>

DO_IO() tracks the number of bytes already transferred in a signed ssize_t, which is also used to report errors. When building with _FORTIFY_SOURCE=3 and LTO, gcc 15 can't prove that "nbyte - ret" stays within the destination buffer, and the build fails on the RPMB data read in do_rpmb_write_block():

In function 'read',
inlined from 'do_rpmb_write_block' at mmc_cmds.c:...
bits/unistd.h:32:10: error: '__read_alias' writing 484 or more bytes into a region of size 256 overflows the destination [-Werror=stringop-overflow=]

The loop can't actually overflow, so this is a false positive. Track the progress with a size_t, which can't go negative inside the loop, and only convert to the ssize_t return value at the end. The behaviour is
unchanged: EINTR is retried, other errors return -1, and EOF returns the short count.

Quoted 4 lines

This is the default hardening configuration on Ubuntu, so mmc-utils currently fails to build there.

Applied for master to mmc-utils, thanks!

Kind regards
Uffe

Quoted 50 lines

---

---
 mmc_cmds.c | 19 +++++++++++--------
 1 file changed, 11 insertions(+), 8 deletions(-)
mmc_cmds.c +11 -9
index 2d9069f..8e21a25 100644
@@ -2207,18 +2207,21 @@ int do_sanitize(int nargs, char **argv)
 
 #define DO_IO(func, fd, buf, nbyte)                                    \
        ({                                                                                              \
-               ssize_t ret = 0, r;                                                     \
+               size_t done = 0;                                                        \
+               ssize_t r;                                                                      \
                do {                                                                            \
-                       r = func(fd, buf + ret, nbyte - ret);   \
-                       if (r < 0 && errno != EINTR) {                  \
-                               ret = -1;                                                       \
+                       r = func(fd, (char *)(buf) + done,              \
+                                (nbyte) - done);                                       \
+                       if (r < 0) {                                                    \
+                               if (errno == EINTR)                                     \
+                                       continue;                                               \
+                               done = (size_t)-1;                                      \
                                break;                                                          \
                        }                                                                               \
-                       else if (r > 0)                                                 \
-                               ret += r;                                                       \
-               } while (r != 0 && (size_t)ret < nbyte);        \
+                       done += (size_t)r;                                              \
+               } while (r != 0 && done < (size_t)(nbyte));     \
                                                                                                        \
-               ret;                                                                            \
+               (ssize_t)done;                                                          \
        })
 
 #define RPMB_MULTI_CMD_MAX_CMDS 3
 
---

base-commit: c6692d5f9bf1ab6fbf5694a0b61477621cc4d016
change-id: 20261008-do-io-unsigned-counter-568a63baef5c

Best regards,
--
Sebastien Bacher <[email protected]>