#include "string-table.h"
#include "strv.h"
#include "user-util.h"
+#include "utf8.h"
#include "varlink-io.systemd.Hostname.h"
#include "virt.h"
}
static int get_hardware_serial(char **ret) {
- int r;
+ _cleanup_free_ char *b = NULL;
+ int r = 0;
+
+ FOREACH_STRING(attr, "product_serial", "board_serial") {
+ r = get_hardware_firmware_data(attr, &b);
+ if (r != 0 && !ERRNO_IS_NEG_DEVICE_ABSENT(r))
+ break;
+ }
+ if (r < 0)
+ return r;
+ if (r == 0)
+ return -ENOENT;
- r = get_hardware_firmware_data("product_serial", ret);
- if (r <= 0)
- return get_hardware_firmware_data("board_serial", ret);
+ /* Do some superficial validation: do not allow CCs and make sure D-Bus won't kick us off the bus
+ * because we send invalid UTF-8 data */
- return r;
+ if (string_has_cc(b, /* ok= */ NULL))
+ return -ENOENT;
+
+ if (!utf8_is_valid(b))
+ return -ENOENT;
+
+ if (ret)
+ *ret = TAKE_PTR(b);
+
+ return 0;
}
static int get_firmware_version(char **ret) {