Add support for loading SMBIOS OEM strings from a file
v2 cover Add support for loading SMBIOS OEM strings from a file
I previously added support for SMBIOS OEM strings tables but only allowed for data to be passed inline. Potential users indicated they wanted to pass some quite large data blobs which is inconvenient todo inline. Thus I'm adding support for passing the data from a file.
In testing this I discovered the hard way that on x86 we're limited to using the SMBIOS 2.1 entry point currently. This has a maximum size of 0xffff, and if you exceed this all sorts of wierd behaviour happens.
QEMU forces SMBIOS 2.1 on x86 because the default SeaBIOS firmware does not support SMBIOS 3.0. The EDK2 firmware supports SMBIOS 3.0 and QEMU defaults to this on the ARM virt machine type.
This series adds support for checking the SMBIOS 2.1 limits to protect users from impossible to diagnose problems.
There is also a fix needed to SeaBIOS which fails to check for integer overflow when it appends the type 0 table.
https://mail.coreboot.org/hyperkitty/list/[email protected]/thread/3EMIOY=
6YS6MG5UQN3JJJS2A3DJZOVFR6/
IIUC, SMBIOS 3.0 should only be limited by what you can fit into RAM, but in testing, EDK2 appears to hang shortly after the SMBIOS 3.0 data size exceeds 128 KB. I've not spotted an obvious flaw in EDK2 or QEMU, nor do I attempt to enforce a limit in QEMU for SMBIOS 3.0.
Changed in v2:
- Drop the patches that allowed choice of SMBIOS 2.1 / 3.0 entry point. There's no compelling reason to need 3.0 on x86 while aarch64 is 3.0 only.
Daniel P. Berrang=C3=A9 (3):
hw/smbios: support loading OEM strings values from a file hw/smbios: report error if table size is too large
qemu-options: document SMBIOS type 11 settings
hw/smbios/smbios.c | 86 +++++++++++++++++++++++++++++++++++++++------- qemu-options.hx | 41 ++++++++++++++++++++++ 2 files changed, 114 insertions(+), 13 deletions(-)
--=20
2.26.2
PATCH v2 1/3 hw/smbios: support loading OEM strings values from a file
Some applications want to pass quite large values for the OEM strings entries. Rather than having huge strings on the command line, it would be better to load them from a file, as supported with -fw_cfg.
This introduces the "path" parameter allowing for:
$ echo -n "thisthing" > mydata.txt
$ qemu-system-x86_64 \
-smbios type=11,value=something \
-smbios type=11,path=mydata.txt \
-smbios type=11,value=somemore \
...other args...Now in the guest
$ dmidecode -t 11 Getting SMBIOS data from sysfs. SMBIOS 2.8 present.
Handle 0x0E00, DMI type 11, 5 bytes
OEM Strings
String 1: something
String 2: thisthing
String 3: somemore
--- hw/smbios/smbios.c | 72 +++++++++++++++++++++++++++++++++++++--------- 1 file changed, 59 insertions(+), 13 deletions(-)
hw/smbios/smbios.c +59 -13
@@ -110,7 +110,7 @@ static struct {static struct {size_t nvalues; -const char **values; +char **values; } type11; static struct {
@@ -314,6 +314,11 @@ static const QemuOptDesc qemu_smbios_type11_opts[] = { .type = QEMU_OPT_STRING, .help = "OEM string data", }, + { + .name = "path", + .type = QEMU_OPT_STRING, + .help = "OEM string data from file", + }, };static const QemuOptDesc qemu_smbios_type17_opts[] = {
@@ -641,6 +646,8 @@ static void smbios_build_type_11_table(void)for (i =0 ; i < type11.nvalues; i++) {SMBIOS_TABLE_SET_STR_LIST (11 , type11.values[i]); +g_free (type11.values[i]); + type11.values[i] =NULL ; }SMBIOS_BUILD_TABLE_POST ;
@@ -940,9 +947,8 @@ static void save_opt(const char **dest, QemuOpts *opts, const char *name)struct opt_list { -const char *name;size_t *ndest; -const char ***dest; +char ***dest; }; static int save_opt_one(void *opaque,
@@ -951,23 +957,61 @@ static int save_opt_one(void *opaque, {struct opt_list *opt = opaque; -if (!g_str_equal (name, opt->name)) { -return 0 ; +if (g_str_equal (name,"path" )) { +g_autoptr (GByteArray) data =g_byte_array_new (); + g_autofree char *buf = g_new(char, 4096); +ssize_t ret; +int fd =qemu_open (value,O_RDONLY ); +if (fd <0 ) { +error_setg (errp,"Unable to open %s: %s" , value,strerror (errno)); +return -1 ; + } + +while (1 ) { + ret =read (fd, buf,4096 ); +if (ret ==0 ) { +break ; + } +if (ret <0 ) { +error_setg (errp,"Unable to read from %s: %s" , + value,strerror (errno)); +return -1 ; + } +if (memchr (buf,'\0' , ret)) { +error_setg (errp,"NUL in OEM strings value in %s" , value); +return -1 ; + } +g_byte_array_append (data, (guint8 *)buf, ret); + } + +close (fd); + + *opt->dest = g_renew(char *, *opt->dest, (*opt->ndest) + 1); + (*opt->dest)[*opt->ndest] = (char *)g_byte_array_free (data, FALSE); + (*opt->ndest)++; + data =NULL ; + }else if (g_str_equal (name,"value" )) { + *opt->dest = g_renew(char *, *opt->dest, (*opt->ndest) + 1); + (*opt->dest)[*opt->ndest] =g_strdup (value); + (*opt->ndest)++; + }else if (!g_str_equal (name,"type" )) { +error_setg (errp,"Unexpected option %s" , name); +return -1 ; } - *opt->dest = g_renew(const char *, *opt->dest, (*opt->ndest) + 1); - (*opt->dest)[*opt->ndest] = value; - (*opt->ndest)++;return 0 ; } -static void save_opt_list (size_t *ndest,const char ***dest, -QemuOpts *opts,const char *name) +static bool save_opt_list (size_t *ndest,char ***dest,QemuOpts *opts, +Error **errp) {struct opt_list opt = { - name, ndest, dest, + ndest, dest, }; -qemu_opt_foreach (opts, save_opt_one, &opt,NULL ); +if (!qemu_opt_foreach (opts, save_opt_one, &opt, errp)) { +return false; + } +return true; }void smbios_entry_add (QemuOpts *opts,Error **errp)
@@ -1149,7 +1193,9 @@ void smbios_entry_add(QemuOpts *opts, Error **errp)if (!qemu_opts_validate (opts, qemu_smbios_type11_opts, errp)) {return ; } -save_opt_list (&type11.nvalues, &type11.values, opts,"value" ); +if (!save_opt_list (&type11.nvalues, &type11.values, opts, errp)) { +return ; + }return ;case 17 :if (!qemu_opts_validate (opts, qemu_smbios_type17_opts, errp)) {
Signature · 1 line
2.26.2
Re: [PATCH v2 1/3] hw/smbios: support loading OEM strings values from a file
Hi Daniel,
Quoted 77 lines · Daniel P. Berrangé
Some applications want to pass quite large values for the OEM strings entries. Rather than having huge strings on the command line, it would be better to load them from a file, as supported with -fw_cfg.
This introduces the "path" parameter allowing for:
$ echo -n "thisthing" > mydata.txt $ qemu-system-x86_64 \ -smbios type=11,value=something \ -smbios type=11,path=mydata.txt \ -smbios type=11,value=somemore \ ...other args...Now in the guest
$ dmidecode -t 11 Getting SMBIOS data from sysfs. SMBIOS 2.8 present.Handle
0x0E00, DMI type 11, 5 bytes
OEM Strings
String 1: something
String 2: thisthing
String 3: somemoreReviewed-by Philippe Mathieu-Daudé <[email protected]>Signed-off-by Daniel P. Berrangé <[email protected]>--- hw/smbios/smbios.c | 72 +++++++++++++++++++++++++++++++++++++--------- 1 file changed, 59 insertions(+), 13 deletions(-)hw/smbios/smbios.c +9
-3@@ -110,7 +110,7 @@ static struct {static struct {size_t nvalues; -const char **values; +char **values; } type11; static struct {@@ -314,6 +314,11 @@ static const QemuOptDesc qemu_smbios_type11_opts[] = { .type = QEMU_OPT_STRING, .help = "OEM string data", }, + { + .name = "path", + .type = QEMU_OPT_STRING, + .help = "OEM string data from file", + }, };static const QemuOptDesc qemu_smbios_type17_opts[] = {@@ -641,6 +646,8 @@ static void smbios_build_type_11_table(void)for (i =0 ; i < type11.nvalues; i++) {SMBIOS_TABLE_SET_STR_LIST (11 , type11.values[i]); +g_free (type11.values[i]); + type11.values[i] =NULL ; }SMBIOS_BUILD_TABLE_POST ;@@ -940,9 +947,8 @@ static void save_opt(const char **dest, QemuOpts *opts, const char *name)struct opt_list { -const char *name;size_t *ndest; -const char ***dest; +char ***dest; }; static int save_opt_one(void *opaque,
hw/smbios/smbios.c static int save_opt_one(void *opaque, · line 964
Quoted 10 lines
hw/smbios/smbios.c +5
-2@@ -951,23 +957,61 @@ static int save_opt_one(void *opaque, {struct opt_list *opt = opaque; -if (!g_str_equal (name, opt->name)) { -return 0 ; +if (g_str_equal (name,"path" )) { +g_autoptr (GByteArray) data =g_byte_array_new (); + g_autofree char *buf = g_new(char, 4096); +ssize_t ret; +int fd =qemu_open (value,O_RDONLY );
This line now fails to compile, due to commit c490af57cb45 ("util:
introduce qemu_open and qemu_create with error reporting", 2020-09-16).
... I guess I could test the patch with qemu_open_old(), but that wouldn't allow for a valid Tested-by.
Thanks,
Laszlo
Quoted 72 lines
hw/smbios/smbios.c +45
-8+if (fd <0 ) { +error_setg (errp,"Unable to open %s: %s" , value,strerror (errno)); +return -1 ; + } + +while (1 ) { + ret =read (fd, buf,4096 ); +if (ret ==0 ) { +break ; + } +if (ret <0 ) { +error_setg (errp,"Unable to read from %s: %s" , + value,strerror (errno)); +return -1 ; + } +if (memchr (buf,'\0' , ret)) { +error_setg (errp,"NUL in OEM strings value in %s" , value); +return -1 ; + } +g_byte_array_append (data, (guint8 *)buf, ret); + } + +close (fd); + + *opt->dest = g_renew(char *, *opt->dest, (*opt->ndest) + 1); + (*opt->dest)[*opt->ndest] = (char *)g_byte_array_free (data, FALSE); + (*opt->ndest)++; + data =NULL ; + }else if (g_str_equal (name,"value" )) { + *opt->dest = g_renew(char *, *opt->dest, (*opt->ndest) + 1); + (*opt->dest)[*opt->ndest] =g_strdup (value); + (*opt->ndest)++; + }else if (!g_str_equal (name,"type" )) { +error_setg (errp,"Unexpected option %s" , name); +return -1 ; } - *opt->dest = g_renew(const char *, *opt->dest, (*opt->ndest) + 1); - (*opt->dest)[*opt->ndest] = value; - (*opt->ndest)++;return 0 ; } -static void save_opt_list (size_t *ndest,const char ***dest, -QemuOpts *opts,const char *name) +static bool save_opt_list (size_t *ndest,char ***dest,QemuOpts *opts, +Error **errp) {struct opt_list opt = { - name, ndest, dest, + ndest, dest, }; -qemu_opt_foreach (opts, save_opt_one, &opt,NULL ); +if (!qemu_opt_foreach (opts, save_opt_one, &opt, errp)) { +return false; + } +return true; }void smbios_entry_add (QemuOpts *opts,Error **errp)@@ -1149,7 +1193,9 @@ void smbios_entry_add(QemuOpts *opts, Error **errp)if (!qemu_opts_validate (opts, qemu_smbios_type11_opts, errp)) {return ; } -save_opt_list (&type11.nvalues, &type11.values, opts,"value" ); +if (!save_opt_list (&type11.nvalues, &type11.values, opts, errp)) { +return ; + }return ;case 17 :if (!qemu_opts_validate (opts, qemu_smbios_type17_opts, errp)) {
PATCH v2 2/3 hw/smbios: report error if table size is too large
In reply to Daniel P. BerrangéThe SMBIOS 2.1 entry point uses a uint16 data type for reporting the total length of the tables. If the user passes -smbios configuration to QEMU that causes the table size to exceed this limit then various bad behaviours result, including
- firmware hangs in an infinite loop
- firmware triggers a KVM crash on bad memory access
- firmware silently discards user's SMBIOS data replacing it with a generic data set.
Limiting the size to 0xffff in QEMU avoids triggering most of these problems. There is a remaining bug in SeaBIOS which tries to prepend its own data for table 0, and does not check whether there is sufficient space before attempting this.
--- hw/smbios/smbios.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+)
hw/smbios/smbios.c +14 -0
@@ -365,6 +365,13 @@ static void smbios_register_config(void)opts_init (smbios_register_config); +/* +* The SMBIOS 2.1 "structure table length" field in the +* entry point uses a 16-bit integer, so we're limited +* in total table size +*/ +#define SMBIOS_21_MAX_TABLES_LEN 0xffff +static void smbios_validate_table (MachineState *ms) { uint32_t expect_t4_count = smbios_legacy ?
@@ -375,6 +382,13 @@ static void smbios_validate_table(MachineState *ms) expect_t4_count, smbios_type4_count);exit (1 ); } + +if (smbios_ep_type ==SMBIOS_ENTRY_POINT_21 && + smbios_tables_len >SMBIOS_21_MAX_TABLES_LEN ) { +error_report ("SMBIOS 2.1 table length %zu exceeds %d" , + smbios_tables_len,SMBIOS_21_MAX_TABLES_LEN ); +exit (1 ); + } }
Signature · 1 line
2.26.2
PATCH v2 3/3 qemu-options: document SMBIOS type 11 settings
In reply to Daniel P. Berrangé--- qemu-options.hx | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+)
qemu-options.hx +41 -0
@@ -2296,6 +2296,8 @@ DEF("smbios", HAS_ARG, QEMU_OPTION_smbios, "-smbios type=4[,sock_pfx=str][,manufacturer=str][,version=str][,serial=str]\n" " [,asset=str][,part=str][,max-speed=%d][,current-speed=%d]\n" " specify SMBIOS type 4 fields\n" + "-smbios type=11[,value=str][,path=filename]\n" + " specify SMBIOS type 11 fields\n" "-smbios type=17[,loc_pfx=str][,bank=str][,manufacturer=str][,serial=str]\n" " [,asset=str][,part=str][,speed=%d]\n" " specify SMBIOS type 17 fields\n",
@@ -2319,6 +2321,45 @@ SRST ``-smbios type=4[,sock_pfx=str][,manufacturer=str][,version=str][,serial=str][,asset=str][,part=str]`` Specify SMBIOS type 4 fields +``-smbios type=11[,value=str][,path=filename]`` + Specify SMBIOS type 11 fields inline + + This argument can be repeated multiple times, and values are added in the order they are parsed. + Applications intending to use OEM strings data are encouraged to use their application name as + a prefix for the value string. This facilitates passing information for multiple applications + concurrently. + + The ``value=str`` syntax provides the string data inline, while the ``path=filename`` syntax + loads data from a file on disk. Note that the file is not permitted to contain any NUL bytes. + + Both the ``value`` and ``path`` options can be repeated multiple times and will be added to + the SMBIOS table in the order in which they appear. + + Note that on the x86 architecture, the total size of all SMBIOS tables is limited to 65535 + bytes. Thus the OEM strings data is not suitable for passing large amounts of data into the + guest. Instead it should be used as a indicator to inform the guest where to locate the real + data set, for example, by specifying the serial ID of a block device. + + An example passing three strings is + + .. parsed-literal:: + + -smbios type=11,value=cloud-init:ds=nocloud-net;s=http://10.10.0.1:8000/,\\ + value=anaconda:method=http://dl.fedoraproject.org/pub/fedora/linux/releases/25/x86_64/os,\\ + path=/some/file/with/oemstringsdata.txt + + In the guest OS this is visible with the ``dmidecode`` command + + .. parsed-literal:: + + $ dmidecode -t 11 + Handle 0x0E00, DMI type 11, 5 bytes + OEM Strings + String 1: cloud-init:ds=nocloud-net;s=http://10.10.0.1:8000/ + String 2: anaconda:method=http://dl.fedoraproject.org/pub/fedora/linux/releases/25/x86_64/os + String 3: myapp:some extra data + + ``-smbios type=17[,loc_pfx=str][,bank=str][,manufacturer=str][,serial=str][,asset=str][,part=str][,speed=%d]`` Specify SMBIOS type 17 fields ERST
Signature · 1 line
2.26.2
Patchew URL: https://patchew.org/QEMU/[email protected]/
Patchew URL: https://patchew.org/QEMU/[email protected]/
Hi,
This series failed the docker-mingw@fedora build test. Please find the testing commands and their output below. If you have Docker installed, you can probably reproduce it locally.
=== TEST SCRIPT BEGIN ===
#! /bin/bash
export ARCH=x86_64
make docker-image-fedora V=1 NETWORK=1
time make docker-test-mingw@fedora J=14 NETWORK=1
=== TEST SCRIPT END ===
Host machine cpu: x86_64
Target machine cpu family: x86
Target machine cpu: x86_64
../src/meson.build:10: WARNING: Module unstable-keyval has no backwards or forwards compatibility and might not exist in future releases. Program sh found: YES
Program python3 found: YES (/usr/bin/python3)
Configuring ninjatool using configuration
---
done
copying static files... ... Compiling C object libqemu-x86_64-softmmu.fa.p/target_i386_seg_helper.c.obj ../src/hw/smbios/smbios.c: In function 'save_opt_one':../src/hw/smbios/smbios.c:978:18: error: too few arguments to function 'qemu_open'
978 | int fd = qemu_open(value, O_RDONLY);
| ^~~~~~~~~
In file included from ../src/hw/smbios/smbios.c:18:
/tmp/qemu-test/src/include/qemu/osdep.h:505:5: note: declared here
505 | int qemu_open(const char *name, int flags, Error **errp);
| ^~~~~~~~~make: *** [Makefile.ninja:1391: libcommon.fa.p/hw_smbios_smbios.c.obj] Error 1
make: *** Waiting for unfinished jobs....
done
copying extra files... done
---
raise CalledProcessError(retcode, cmd)
subprocess.CalledProcessError: Command '['sudo', '-n', 'docker', 'run', '--rm', '--label', 'com.qemu.instance.uuid=2a6817d121314e55a2f8ae80f641f8d8', '-u', '1003', '--security-opt', 'seccomp=unconfined', '-e', 'TARGET_LIST=', '-e', 'EXTRA_CONFIGURE_OPTS=', '-e', 'V=', '-e', 'J=14', '-e', 'DEBUG=', '-e', 'SHOW_ENV=', '-e', 'CCACHE_DIR=/var/tmp/ccache', '-v', '/home/patchew2/.cache/qemu-docker-ccache:/var/tmp/ccache:z', '-v', '/var/tmp/patchew-tester-tmp-_uibmeyt/src/docker-src.2020-09-23-09.16.19.12753:/var/tmp/qemu:z,ro', 'qemu/fedora', '/var/tmp/qemu/run', 'test-mingw']' returned non-zero exit status 2. filter=--filter=label=com.qemu.instance.uuid=2a6817d121314e55a2f8ae80f641f8d8
make[1]: *** [docker-run] Error 1
make[1]: Leaving directory `/var/tmp/patchew-tester-tmp-_uibmeyt/src'
make: *** [docker-run-test-mingw@fedora] Error 2
real 3m26.585s
user 0m21.696s
The full log is available at
http://patchew.org/logs/[email protected]/testing.docker-mingw@fedora/?type=message.
---
Email generated automatically by Patchew [https://patchew.org/].
Please send your feedback to [email protected]
Patchew URL: https://patchew.org/QEMU/[email protected]/
Patchew URL: https://patchew.org/QEMU/[email protected]/
Hi,
This series failed the docker-quick@centos7 build test. Please find the testing commands and their output below. If you have Docker installed, you can probably reproduce it locally.
=== TEST SCRIPT BEGIN ===
#!/bin/bash
make docker-image-centos7 V=1 NETWORK=1
time make docker-test-quick@centos7 SHOW_ENV=1 J=14 NETWORK=1 === TEST SCRIPT END ===
C linker for the host machine: cc ld.bfd 2.27-43
Host machine cpu family: x86_64
Host machine cpu: x86_64
../src/meson.build:10: WARNING: Module unstable-keyval has no backwards or forwards compatibility and might not exist in future releases. Program sh found: YES
Program python3 found: YES (/usr/bin/python3)
Configuring ninjatool using configuration
---
Compiling C object libcommon.fa.p/hw_timer_hpet.c.o
Compiling C object libcommon.fa.p/ui_input-keymap.c.o../src/hw/smbios/smbios.c: In function 'save_opt_one':../src/hw/smbios/smbios.c:978:9: error: too few arguments to function 'qemu_open'
int fd = qemu_open(value, O_RDONLY);
^
In file included from ../src/hw/smbios/smbios.c:18:0:
/tmp/qemu-test/src/include/qemu/osdep.h:505:5: note: declared here
int qemu_open(const char *name, int flags, Error **errp);
^make: *** [libcommon.fa.p/hw_smbios_smbios.c.o] Error 1
make: *** Waiting for unfinished jobs....
Traceback (most recent call last):
File "./tests/docker/docker.py", line 709, in <module>
---
raise CalledProcessError(retcode, cmd)
subprocess.CalledProcessError: Command '['sudo', '-n', 'docker', 'run', '--rm', '--label', 'com.qemu.instance.uuid=5dd36d109cb849d0af8a1a26424a4d18', '-u', '1003', '--security-opt', 'seccomp=unconfined', '-e', 'TARGET_LIST=', '-e', 'EXTRA_CONFIGURE_OPTS=', '-e', 'V=', '-e', 'J=14', '-e', 'DEBUG=', '-e', 'SHOW_ENV=1', '-e', 'CCACHE_DIR=/var/tmp/ccache', '-v', '/home/patchew2/.cache/qemu-docker-ccache:/var/tmp/ccache:z', '-v', '/var/tmp/patchew-tester-tmp-7ak3dvd1/src/docker-src.2020-09-23-09.20.24.18349:/var/tmp/qemu:z,ro', 'qemu/centos7', '/var/tmp/qemu/run', 'test-quick']' returned non-zero exit status 2. filter=--filter=label=com.qemu.instance.uuid=5dd36d109cb849d0af8a1a26424a4d18
make[1]: *** [docker-run] Error 1
make[1]: Leaving directory `/var/tmp/patchew-tester-tmp-7ak3dvd1/src'
make: *** [docker-run-test-quick@centos7] Error 2
real 3m30.634s
user 0m22.614s
The full log is available at
http://patchew.org/logs/[email protected]/testing.docker-quick@centos7/?type=message.
---
Email generated automatically by Patchew [https://patchew.org/].
Please send your feedback to [email protected]