From c3c41d4ffbdbbce2e7199ece8f76cea0310de820 Mon Sep 17 00:00:00 2001
From: Theodore Ts'o <tytso@mit.edu>
Date: Thu, 21 Jan 2021 23:27:00 -0500
Subject: [PATCH] libext2fs: fix UBSAN warning in ext2fs_mmp_new_seq()

Left shifting the pid by 16 bits can cause a UBSAN warning if the pid
is greater than or equal to 2**16.  It doesn't matter since we're just
using the pid to seed for a pseudo-random number generator, but
silence the warning by just swapping the high and low 16 bits of the
pid instead.

Signed-off-by: Theodore Ts'o <tytso@mit.edu>
---
 lib/ext2fs/mmp.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/lib/ext2fs/mmp.c b/lib/ext2fs/mmp.c
index e96a2273..223b617d 100644
--- a/lib/ext2fs/mmp.c
+++ b/lib/ext2fs/mmp.c
@@ -172,9 +172,11 @@ unsigned ext2fs_mmp_new_seq(void)
 #ifdef CONFIG_MMP
 	unsigned new_seq;
 	struct timeval tv;
+	unsigned long pid = getpid();
 
 	gettimeofday(&tv, 0);
-	srand((getpid() << 16) ^ getuid() ^ tv.tv_sec ^ tv.tv_usec);
+	pid = (pid >> 16) | ((pid & 0xFFFF) << 16);
+	srand(pid ^ getuid() ^ tv.tv_sec ^ tv.tv_usec);
 
 	gettimeofday(&tv, 0);
 	/* Crank the random number generator a few times */
-- 
2.25.1

