|
1 | | -/* $OpenBSD: ssh.c,v 1.598 2023/10/12 02:48:43 djm Exp $ */ |
| 1 | +/* $OpenBSD: ssh.c,v 1.599 2023/12/18 14:47:44 djm Exp $ */ |
2 | 2 | /* |
3 | 3 | * Author: Tatu Ylonen <[email protected]> |
4 | 4 | * Copyright (c) 1995 Tatu Ylonen <[email protected]>, Espoo, Finland |
@@ -626,6 +626,41 @@ ssh_conn_info_free(struct ssh_conn_info *cinfo) |
626 | 626 | free(cinfo); |
627 | 627 | } |
628 | 628 |
|
| 629 | +static int |
| 630 | +valid_hostname(const char *s) |
| 631 | +{ |
| 632 | + size_t i; |
| 633 | + |
| 634 | + if (*s == '-') |
| 635 | + return 0; |
| 636 | + for (i = 0; s[i] != 0; i++) { |
| 637 | + if (strchr("'`\"$\\;&<>|(){}", s[i]) != NULL || |
| 638 | + isspace((u_char)s[i]) || iscntrl((u_char)s[i])) |
| 639 | + return 0; |
| 640 | + } |
| 641 | + return 1; |
| 642 | +} |
| 643 | + |
| 644 | +static int |
| 645 | +valid_ruser(const char *s) |
| 646 | +{ |
| 647 | + size_t i; |
| 648 | + |
| 649 | + if (*s == '-') |
| 650 | + return 0; |
| 651 | + for (i = 0; s[i] != 0; i++) { |
| 652 | + if (strchr("'`\";&<>|(){}", s[i]) != NULL) |
| 653 | + return 0; |
| 654 | + /* Disallow '-' after whitespace */ |
| 655 | + if (isspace((u_char)s[i]) && s[i + 1] == '-') |
| 656 | + return 0; |
| 657 | + /* Disallow \ in last position */ |
| 658 | + if (s[i] == '\\' && s[i + 1] == '\0') |
| 659 | + return 0; |
| 660 | + } |
| 661 | + return 1; |
| 662 | +} |
| 663 | + |
629 | 664 | /* |
630 | 665 | * Main program for the ssh client. |
631 | 666 | */ |
@@ -1118,6 +1153,10 @@ main(int ac, char **av) |
1118 | 1153 | if (!host) |
1119 | 1154 | usage(); |
1120 | 1155 |
|
| 1156 | + if (!valid_hostname(host)) |
| 1157 | + fatal("hostname contains invalid characters"); |
| 1158 | + if (options.user != NULL && !valid_ruser(options.user)) |
| 1159 | + fatal("remote username contains invalid characters"); |
1121 | 1160 | options.host_arg = xstrdup(host); |
1122 | 1161 |
|
1123 | 1162 | /* Initialize the command to execute on remote host. */ |
|
0 commit comments