zdev: Harden against invalid hypervisor data

Reduce chances of unintended side-effects when evaluating hypervisor
data which might have been corrupted.

Reviewed-by: Vineeth Vijayan <vneethv@linux.ibm.com>
Reviewed-by: Jan Höppner <hoeppner@linux.ibm.com>
Signed-off-by: Peter Oberparleiter <oberpar@linux.ibm.com>
Signed-off-by: Jan Höppner <hoeppner@linux.ibm.com>
This commit is contained in:
Peter Oberparleiter
2026-07-06 14:21:26 +02:00
committed by Jan Höppner
parent 4ca93aa808
commit a048670bec
3 changed files with 47 additions and 3 deletions

View File

@@ -41,9 +41,19 @@ if [ $zdev_auto -eq 1 ] ; then
chzdev --import "$zdev_fw_file" $zdev_base_args
# Get information about DPM environment
for line in $($zdev_id) ; do
eval "$line"
done
tmp="/tmp/zdev_id.env"
if "$zdev_id" >"$tmp"; then
while IFS='=' read -r key val; do
case "$key" in
ZDEV_*)
val=${val#\"}
val=${val%\"}
export "$key=$val"
;;
esac
done <"$tmp"
fi
rm -f "$tmp"
if [ "$ZDEV_IS_DPM,$ZDEV_NEST_LEVEL,$ZDEV_HYPERVISOR_0" = "1,1,LPAR" ] ; then
# Manually iterate over existing PCI devices - there is a udev rule

32
zdev/include/sanitize.h Normal file
View File

@@ -0,0 +1,32 @@
/*
* zdev - Modify and display the persistent configuration of devices
*
* Copyright IBM Corp. 2026
*
* s390-tools is free software; you can redistribute it and/or modify
* it under the terms of the MIT license. See LICENSE for details.
*/
#ifndef SANITIZE_H
#define SANITIZE_H
#include <ctype.h>
#include <string.h>
/* Valid control-program identifier special characters */
#define VALID_CPNAME "/"
static inline int is_safe_char(unsigned char c, const char *set)
{
return isalnum(c) || strchr(set, c);
}
static inline void sanitize(char *s, const char *set)
{
for (; *s; s++) {
if (!is_safe_char((unsigned char)*s, set))
*s = '_';
}
}
#endif /* SANITIZE_H */

View File

@@ -13,6 +13,7 @@
#include <string.h>
#include <ctype.h>
#include "sanitize.h"
#include "zdev.h"
#include "zdev_id.h"
@@ -69,6 +70,7 @@ static void process_sysinfo(const char *filename)
free(substr);
} else if (sscanf(line, "VM%*d Control Program: %ms ",
&substr) == 1) {
sanitize(substr, VALID_CPNAME);
array_add(&cps, &num_cps, substr);
free(substr);
}