Skip to content

Commit 113f4cd

Browse files
Ian Campbellmdroth
Ian Campbell
authored andcommitted
console: bounds check whenever changing the cursor due to an escape code
This is XSA-17 / CVE-2012-3515 Signed-off-by: Ian Campbell <[email protected]> Signed-off-by: Anthony Liguori <[email protected]>
1 parent f965d23 commit 113f4cd

File tree

1 file changed

+28
-29
lines changed

1 file changed

+28
-29
lines changed

console.c

+28-29
Original file line numberDiff line numberDiff line change
@@ -847,6 +847,26 @@ static void console_clear_xy(TextConsole *s, int x, int y)
847847
update_xy(s, x, y);
848848
}
849849

850+
/* set cursor, checking bounds */
851+
static void set_cursor(TextConsole *s, int x, int y)
852+
{
853+
if (x < 0) {
854+
x = 0;
855+
}
856+
if (y < 0) {
857+
y = 0;
858+
}
859+
if (y >= s->height) {
860+
y = s->height - 1;
861+
}
862+
if (x >= s->width) {
863+
x = s->width - 1;
864+
}
865+
866+
s->x = x;
867+
s->y = y;
868+
}
869+
850870
static void console_putchar(TextConsole *s, int ch)
851871
{
852872
TextCell *c;
@@ -918,7 +938,8 @@ static void console_putchar(TextConsole *s, int ch)
918938
s->esc_params[s->nb_esc_params] * 10 + ch - '0';
919939
}
920940
} else {
921-
s->nb_esc_params++;
941+
if (s->nb_esc_params < MAX_ESC_PARAMS)
942+
s->nb_esc_params++;
922943
if (ch == ';')
923944
break;
924945
#ifdef DEBUG_CONSOLE
@@ -932,59 +953,37 @@ static void console_putchar(TextConsole *s, int ch)
932953
if (s->esc_params[0] == 0) {
933954
s->esc_params[0] = 1;
934955
}
935-
s->y -= s->esc_params[0];
936-
if (s->y < 0) {
937-
s->y = 0;
938-
}
956+
set_cursor(s, s->x, s->y - s->esc_params[0]);
939957
break;
940958
case 'B':
941959
/* move cursor down */
942960
if (s->esc_params[0] == 0) {
943961
s->esc_params[0] = 1;
944962
}
945-
s->y += s->esc_params[0];
946-
if (s->y >= s->height) {
947-
s->y = s->height - 1;
948-
}
963+
set_cursor(s, s->x, s->y + s->esc_params[0]);
949964
break;
950965
case 'C':
951966
/* move cursor right */
952967
if (s->esc_params[0] == 0) {
953968
s->esc_params[0] = 1;
954969
}
955-
s->x += s->esc_params[0];
956-
if (s->x >= s->width) {
957-
s->x = s->width - 1;
958-
}
970+
set_cursor(s, s->x + s->esc_params[0], s->y);
959971
break;
960972
case 'D':
961973
/* move cursor left */
962974
if (s->esc_params[0] == 0) {
963975
s->esc_params[0] = 1;
964976
}
965-
s->x -= s->esc_params[0];
966-
if (s->x < 0) {
967-
s->x = 0;
968-
}
977+
set_cursor(s, s->x - s->esc_params[0], s->y);
969978
break;
970979
case 'G':
971980
/* move cursor to column */
972-
s->x = s->esc_params[0] - 1;
973-
if (s->x < 0) {
974-
s->x = 0;
975-
}
981+
set_cursor(s, s->esc_params[0] - 1, s->y);
976982
break;
977983
case 'f':
978984
case 'H':
979985
/* move cursor to row, column */
980-
s->x = s->esc_params[1] - 1;
981-
if (s->x < 0) {
982-
s->x = 0;
983-
}
984-
s->y = s->esc_params[0] - 1;
985-
if (s->y < 0) {
986-
s->y = 0;
987-
}
986+
set_cursor(s, s->esc_params[1] - 1, s->esc_params[0] - 1);
988987
break;
989988
case 'J':
990989
switch (s->esc_params[0]) {

0 commit comments

Comments
 (0)