Skip to content

Commit 80b1bfe

Browse files
nivedita76ardbiesheuvel
authored andcommitted
efi/libstub: Don't parse overlong command lines
Check if the command line passed in is larger than COMMAND_LINE_SIZE, and truncate it to the last full argument if so. Signed-off-by: Arvind Sankar <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Ard Biesheuvel <[email protected]>
1 parent 04b2440 commit 80b1bfe

File tree

1 file changed

+23
-5
lines changed

1 file changed

+23
-5
lines changed

drivers/firmware/efi/libstub/efi-stub-helper.c

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@
99

1010
#include <stdarg.h>
1111

12+
#include <linux/ctype.h>
1213
#include <linux/efi.h>
1314
#include <linux/kernel.h>
1415
#include <linux/printk.h> /* For CONSOLE_LOGLEVEL_* */
1516
#include <asm/efi.h>
17+
#include <asm/setup.h>
1618

1719
#include "efistub.h"
1820

@@ -217,22 +219,33 @@ char *efi_convert_cmdline(efi_loaded_image_t *image,
217219
unsigned long cmdline_addr = 0;
218220
int options_chars = efi_table_attr(image, load_options_size) / 2;
219221
const u16 *options = efi_table_attr(image, load_options);
220-
int options_bytes = 0; /* UTF-8 bytes */
222+
int options_bytes = 0, safe_options_bytes = 0; /* UTF-8 bytes */
223+
bool in_quote = false;
221224
efi_status_t status;
222225

223226
if (options) {
224227
s2 = options;
225-
while (options_chars--) {
228+
while (options_bytes < COMMAND_LINE_SIZE && options_chars--) {
226229
u16 c = *s2++;
227230

228-
if (c == L'\0' || c == L'\n')
229-
break;
231+
if (c < 0x80) {
232+
if (c == L'\0' || c == L'\n')
233+
break;
234+
if (c == L'"')
235+
in_quote = !in_quote;
236+
else if (!in_quote && isspace((char)c))
237+
safe_options_bytes = options_bytes;
238+
239+
options_bytes++;
240+
continue;
241+
}
242+
230243
/*
231244
* Get the number of UTF-8 bytes corresponding to a
232245
* UTF-16 character.
233246
* The first part handles everything in the BMP.
234247
*/
235-
options_bytes += 1 + (c >= 0x80) + (c >= 0x800);
248+
options_bytes += 2 + (c >= 0x800);
236249
/*
237250
* Add one more byte for valid surrogate pairs. Invalid
238251
* surrogates will be replaced with 0xfffd and take up
@@ -253,6 +266,11 @@ char *efi_convert_cmdline(efi_loaded_image_t *image,
253266
}
254267
}
255268
}
269+
if (options_bytes >= COMMAND_LINE_SIZE) {
270+
options_bytes = safe_options_bytes;
271+
efi_err("Command line is too long: truncated to %d bytes\n",
272+
options_bytes);
273+
}
256274
}
257275

258276
options_bytes++; /* NUL termination */

0 commit comments

Comments
 (0)