]> git.proxmox.com Git - pve-kernel.git/blame - patches/kernel/0049-x86-boot-Add-early-cmdline-parsing-for-options-with-.patch
revert buggy SCSI error handler commit
[pve-kernel.git] / patches / kernel / 0049-x86-boot-Add-early-cmdline-parsing-for-options-with-.patch
CommitLineData
321d628a
FG
1From 0b7f51014f5219ece1ca55662495bd036f3bd00d Mon Sep 17 00:00:00 2001
2From: Tom Lendacky <thomas.lendacky@amd.com>
3Date: Mon, 17 Jul 2017 16:10:33 -0500
633c5ed1 4Subject: [PATCH 049/242] x86/boot: Add early cmdline parsing for options with
321d628a
FG
5 arguments
6MIME-Version: 1.0
7Content-Type: text/plain; charset=UTF-8
8Content-Transfer-Encoding: 8bit
9
10CVE-2017-5754
11
12Add a cmdline_find_option() function to look for cmdline options that
13take arguments. The argument is returned in a supplied buffer and the
14argument length (regardless of whether it fits in the supplied buffer)
15is returned, with -1 indicating not found.
16
17Signed-off-by: Tom Lendacky <thomas.lendacky@amd.com>
18Reviewed-by: Thomas Gleixner <tglx@linutronix.de>
19Cc: Alexander Potapenko <glider@google.com>
20Cc: Andrey Ryabinin <aryabinin@virtuozzo.com>
21Cc: Andy Lutomirski <luto@kernel.org>
22Cc: Arnd Bergmann <arnd@arndb.de>
23Cc: Borislav Petkov <bp@alien8.de>
24Cc: Brijesh Singh <brijesh.singh@amd.com>
25Cc: Dave Young <dyoung@redhat.com>
26Cc: Dmitry Vyukov <dvyukov@google.com>
27Cc: Jonathan Corbet <corbet@lwn.net>
28Cc: Konrad Rzeszutek Wilk <konrad.wilk@oracle.com>
29Cc: Larry Woodman <lwoodman@redhat.com>
30Cc: Linus Torvalds <torvalds@linux-foundation.org>
31Cc: Matt Fleming <matt@codeblueprint.co.uk>
32Cc: Michael S. Tsirkin <mst@redhat.com>
33Cc: Paolo Bonzini <pbonzini@redhat.com>
34Cc: Peter Zijlstra <peterz@infradead.org>
35Cc: Radim Krčmář <rkrcmar@redhat.com>
36Cc: Rik van Riel <riel@redhat.com>
37Cc: Toshimitsu Kani <toshi.kani@hpe.com>
38Cc: kasan-dev@googlegroups.com
39Cc: kvm@vger.kernel.org
40Cc: linux-arch@vger.kernel.org
41Cc: linux-doc@vger.kernel.org
42Cc: linux-efi@vger.kernel.org
43Cc: linux-mm@kvack.org
44Link: http://lkml.kernel.org/r/36b5f97492a9745dce27682305f990fc20e5cf8a.1500319216.git.thomas.lendacky@amd.com
45Signed-off-by: Ingo Molnar <mingo@kernel.org>
46(cherry picked from commit e505371dd83963caae1a37ead9524e8d997341be)
47Signed-off-by: Andy Whitcroft <apw@kathleen.maas>
48(cherry picked from commit 37569cd003aa69a57d5666530436c2d973a57b26)
49Signed-off-by: Andy Whitcroft <apw@canonical.com>
50Signed-off-by: Kleber Sacilotto de Souza <kleber.souza@canonical.com>
51(cherry picked from commit b9f03418aa9b8ecbb1c7f32ac2bfe68fd21de4f5)
52Signed-off-by: Fabian Grünbichler <f.gruenbichler@proxmox.com>
53---
54 arch/x86/include/asm/cmdline.h | 2 +
55 arch/x86/lib/cmdline.c | 105 +++++++++++++++++++++++++++++++++++++++++
56 2 files changed, 107 insertions(+)
57
58diff --git a/arch/x86/include/asm/cmdline.h b/arch/x86/include/asm/cmdline.h
59index e01f7f7ccb0c..84ae170bc3d0 100644
60--- a/arch/x86/include/asm/cmdline.h
61+++ b/arch/x86/include/asm/cmdline.h
62@@ -2,5 +2,7 @@
63 #define _ASM_X86_CMDLINE_H
64
65 int cmdline_find_option_bool(const char *cmdline_ptr, const char *option);
66+int cmdline_find_option(const char *cmdline_ptr, const char *option,
67+ char *buffer, int bufsize);
68
69 #endif /* _ASM_X86_CMDLINE_H */
70diff --git a/arch/x86/lib/cmdline.c b/arch/x86/lib/cmdline.c
71index 5cc78bf57232..3261abb21ef4 100644
72--- a/arch/x86/lib/cmdline.c
73+++ b/arch/x86/lib/cmdline.c
74@@ -104,7 +104,112 @@ __cmdline_find_option_bool(const char *cmdline, int max_cmdline_size,
75 return 0; /* Buffer overrun */
76 }
77
78+/*
79+ * Find a non-boolean option (i.e. option=argument). In accordance with
80+ * standard Linux practice, if this option is repeated, this returns the
81+ * last instance on the command line.
82+ *
83+ * @cmdline: the cmdline string
84+ * @max_cmdline_size: the maximum size of cmdline
85+ * @option: option string to look for
86+ * @buffer: memory buffer to return the option argument
87+ * @bufsize: size of the supplied memory buffer
88+ *
89+ * Returns the length of the argument (regardless of if it was
90+ * truncated to fit in the buffer), or -1 on not found.
91+ */
92+static int
93+__cmdline_find_option(const char *cmdline, int max_cmdline_size,
94+ const char *option, char *buffer, int bufsize)
95+{
96+ char c;
97+ int pos = 0, len = -1;
98+ const char *opptr = NULL;
99+ char *bufptr = buffer;
100+ enum {
101+ st_wordstart = 0, /* Start of word/after whitespace */
102+ st_wordcmp, /* Comparing this word */
103+ st_wordskip, /* Miscompare, skip */
104+ st_bufcpy, /* Copying this to buffer */
105+ } state = st_wordstart;
106+
107+ if (!cmdline)
108+ return -1; /* No command line */
109+
110+ /*
111+ * This 'pos' check ensures we do not overrun
112+ * a non-NULL-terminated 'cmdline'
113+ */
114+ while (pos++ < max_cmdline_size) {
115+ c = *(char *)cmdline++;
116+ if (!c)
117+ break;
118+
119+ switch (state) {
120+ case st_wordstart:
121+ if (myisspace(c))
122+ break;
123+
124+ state = st_wordcmp;
125+ opptr = option;
126+ /* fall through */
127+
128+ case st_wordcmp:
129+ if ((c == '=') && !*opptr) {
130+ /*
131+ * We matched all the way to the end of the
132+ * option we were looking for, prepare to
133+ * copy the argument.
134+ */
135+ len = 0;
136+ bufptr = buffer;
137+ state = st_bufcpy;
138+ break;
139+ } else if (c == *opptr++) {
140+ /*
141+ * We are currently matching, so continue
142+ * to the next character on the cmdline.
143+ */
144+ break;
145+ }
146+ state = st_wordskip;
147+ /* fall through */
148+
149+ case st_wordskip:
150+ if (myisspace(c))
151+ state = st_wordstart;
152+ break;
153+
154+ case st_bufcpy:
155+ if (myisspace(c)) {
156+ state = st_wordstart;
157+ } else {
158+ /*
159+ * Increment len, but don't overrun the
160+ * supplied buffer and leave room for the
161+ * NULL terminator.
162+ */
163+ if (++len < bufsize)
164+ *bufptr++ = c;
165+ }
166+ break;
167+ }
168+ }
169+
170+ if (bufsize)
171+ *bufptr = '\0';
172+
173+ return len;
174+}
175+
176 int cmdline_find_option_bool(const char *cmdline, const char *option)
177 {
178 return __cmdline_find_option_bool(cmdline, COMMAND_LINE_SIZE, option);
179 }
180+
181+int cmdline_find_option(const char *cmdline, const char *option, char *buffer,
182+ int bufsize)
183+{
184+ return __cmdline_find_option(cmdline, COMMAND_LINE_SIZE, option,
185+ buffer, bufsize);
186+}
187--
1882.14.2
189