Skip to contentSkip to conversation

libelf: avoid UB in elf_xen_feature_{get,set}()

PATCH In review Jan Beulich 3 participants 3 messages Jun 20, 2024 – Jun 21, 2024

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 libelf: avoid UB in elf_xen_feature_{get,set}()

When the left shift amount is up to 31, the shifted quantity wants to be of unsigned int (or wider) type.

While there also adjust types: get doesn't alter the array and returns a boolean, while both don't really accept negative "nr". Drop a stray blank each as well.

---
Really I wonder why these exist at all; they're effectively test_bit() and __set_bit() in hypervisor terms, and iirc something like that exists in the tool stack as well.

xen/include/xen/libelf.h +4 -4
@@ -445,13 +445,13 @@ struct elf_dom_parms {
     uint64_t virt_kend;
 };
 
-static inline void elf_xen_feature_set(int nr, uint32_t * addr)
+static inline void elf_xen_feature_set(unsigned int nr, uint32_t *addr)
 {
-    addr[nr >> 5] |= 1 << (nr & 31);
+    addr[nr >> 5] |= 1U << (nr & 31);
 }
-static inline int elf_xen_feature_get(int nr, uint32_t * addr)
+static inline bool elf_xen_feature_get(unsigned int nr, const uint32_t *addr)
 {
-    return !!(addr[nr >> 5] & (1 << (nr & 31)));
+    return addr[nr >> 5] & (1U << (nr & 31));
 }
 
 elf_errorstatus elf_xen_parse_features(const char *features,
Reviewed-by on v1 1/1
#1 lore raw
Quoted 8 lines · Jan Beulich

When the left shift amount is up to 31, the shifted quantity wants to be of unsigned int (or wider) type.

While there also adjust types: get doesn't alter the array and returns a boolean, while both don't really accept negative "nr". Drop a stray blank each as well.

+1 for 4.19.

Quoted 4 lines

---
Really I wonder why these exist at all; they're effectively test_bit() and __set_bit() in hypervisor terms, and iirc something like that exists in the tool stack as well.

The toolstack has tools/libs/ctrl/xc_bitops.h but they're not API compatible with Xen.

They're long-granular rather than int-granular, have swapped arguments, and are non-LOCKed.

~Andrew

#2 lore raw
Quoted 10 lines · Andrew Cooper
Quoted 9 lines · Jan Beulich

When the left shift amount is up to 31, the shifted quantity wants to be
of unsigned int (or wider) type.

While there also adjust types: get doesn't alter the array and returns a
boolean, while both don't really accept negative "nr". Drop a stray blank each as well.

Quoted 5 lines
Quoted 1 line

+1 for 4.19.

~ Oleksii

Quoted 16 lines
Quoted 7 lines

---
Really I wonder why these exist at all; they're effectively test_bit()
and __set_bit() in hypervisor terms, and iirc something like that exists
in the tool stack as well.

The toolstack has tools/libs/ctrl/xc_bitops.h but they're not API compatible with Xen.

They're long-granular rather than int-granular, have swapped arguments,
and are non-LOCKed.

~Andrew