18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * linux/fs/binfmt_script.c 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright (C) 1996 Martin von Löwis 68c2ecf20Sopenharmony_ci * original #!-checking implemented by tytso. 78c2ecf20Sopenharmony_ci */ 88c2ecf20Sopenharmony_ci 98c2ecf20Sopenharmony_ci#include <linux/module.h> 108c2ecf20Sopenharmony_ci#include <linux/string.h> 118c2ecf20Sopenharmony_ci#include <linux/stat.h> 128c2ecf20Sopenharmony_ci#include <linux/binfmts.h> 138c2ecf20Sopenharmony_ci#include <linux/init.h> 148c2ecf20Sopenharmony_ci#include <linux/file.h> 158c2ecf20Sopenharmony_ci#include <linux/err.h> 168c2ecf20Sopenharmony_ci#include <linux/fs.h> 178c2ecf20Sopenharmony_ci 188c2ecf20Sopenharmony_cistatic inline bool spacetab(char c) { return c == ' ' || c == '\t'; } 198c2ecf20Sopenharmony_cistatic inline const char *next_non_spacetab(const char *first, const char *last) 208c2ecf20Sopenharmony_ci{ 218c2ecf20Sopenharmony_ci for (; first <= last; first++) 228c2ecf20Sopenharmony_ci if (!spacetab(*first)) 238c2ecf20Sopenharmony_ci return first; 248c2ecf20Sopenharmony_ci return NULL; 258c2ecf20Sopenharmony_ci} 268c2ecf20Sopenharmony_cistatic inline const char *next_terminator(const char *first, const char *last) 278c2ecf20Sopenharmony_ci{ 288c2ecf20Sopenharmony_ci for (; first <= last; first++) 298c2ecf20Sopenharmony_ci if (spacetab(*first) || !*first) 308c2ecf20Sopenharmony_ci return first; 318c2ecf20Sopenharmony_ci return NULL; 328c2ecf20Sopenharmony_ci} 338c2ecf20Sopenharmony_ci 348c2ecf20Sopenharmony_cistatic int load_script(struct linux_binprm *bprm) 358c2ecf20Sopenharmony_ci{ 368c2ecf20Sopenharmony_ci const char *i_name, *i_sep, *i_arg, *i_end, *buf_end; 378c2ecf20Sopenharmony_ci struct file *file; 388c2ecf20Sopenharmony_ci int retval; 398c2ecf20Sopenharmony_ci 408c2ecf20Sopenharmony_ci /* Not ours to exec if we don't start with "#!". */ 418c2ecf20Sopenharmony_ci if ((bprm->buf[0] != '#') || (bprm->buf[1] != '!')) 428c2ecf20Sopenharmony_ci return -ENOEXEC; 438c2ecf20Sopenharmony_ci 448c2ecf20Sopenharmony_ci /* 458c2ecf20Sopenharmony_ci * This section handles parsing the #! line into separate 468c2ecf20Sopenharmony_ci * interpreter path and argument strings. We must be careful 478c2ecf20Sopenharmony_ci * because bprm->buf is not yet guaranteed to be NUL-terminated 488c2ecf20Sopenharmony_ci * (though the buffer will have trailing NUL padding when the 498c2ecf20Sopenharmony_ci * file size was smaller than the buffer size). 508c2ecf20Sopenharmony_ci * 518c2ecf20Sopenharmony_ci * We do not want to exec a truncated interpreter path, so either 528c2ecf20Sopenharmony_ci * we find a newline (which indicates nothing is truncated), or 538c2ecf20Sopenharmony_ci * we find a space/tab/NUL after the interpreter path (which 548c2ecf20Sopenharmony_ci * itself may be preceded by spaces/tabs). Truncating the 558c2ecf20Sopenharmony_ci * arguments is fine: the interpreter can re-read the script to 568c2ecf20Sopenharmony_ci * parse them on its own. 578c2ecf20Sopenharmony_ci */ 588c2ecf20Sopenharmony_ci buf_end = bprm->buf + sizeof(bprm->buf) - 1; 598c2ecf20Sopenharmony_ci i_end = strnchr(bprm->buf, sizeof(bprm->buf), '\n'); 608c2ecf20Sopenharmony_ci if (!i_end) { 618c2ecf20Sopenharmony_ci i_end = next_non_spacetab(bprm->buf + 2, buf_end); 628c2ecf20Sopenharmony_ci if (!i_end) 638c2ecf20Sopenharmony_ci return -ENOEXEC; /* Entire buf is spaces/tabs */ 648c2ecf20Sopenharmony_ci /* 658c2ecf20Sopenharmony_ci * If there is no later space/tab/NUL we must assume the 668c2ecf20Sopenharmony_ci * interpreter path is truncated. 678c2ecf20Sopenharmony_ci */ 688c2ecf20Sopenharmony_ci if (!next_terminator(i_end, buf_end)) 698c2ecf20Sopenharmony_ci return -ENOEXEC; 708c2ecf20Sopenharmony_ci i_end = buf_end; 718c2ecf20Sopenharmony_ci } 728c2ecf20Sopenharmony_ci /* Trim any trailing spaces/tabs from i_end */ 738c2ecf20Sopenharmony_ci while (spacetab(i_end[-1])) 748c2ecf20Sopenharmony_ci i_end--; 758c2ecf20Sopenharmony_ci 768c2ecf20Sopenharmony_ci /* Skip over leading spaces/tabs */ 778c2ecf20Sopenharmony_ci i_name = next_non_spacetab(bprm->buf+2, i_end); 788c2ecf20Sopenharmony_ci if (!i_name || (i_name == i_end)) 798c2ecf20Sopenharmony_ci return -ENOEXEC; /* No interpreter name found */ 808c2ecf20Sopenharmony_ci 818c2ecf20Sopenharmony_ci /* Is there an optional argument? */ 828c2ecf20Sopenharmony_ci i_arg = NULL; 838c2ecf20Sopenharmony_ci i_sep = next_terminator(i_name, i_end); 848c2ecf20Sopenharmony_ci if (i_sep && (*i_sep != '\0')) 858c2ecf20Sopenharmony_ci i_arg = next_non_spacetab(i_sep, i_end); 868c2ecf20Sopenharmony_ci 878c2ecf20Sopenharmony_ci /* 888c2ecf20Sopenharmony_ci * If the script filename will be inaccessible after exec, typically 898c2ecf20Sopenharmony_ci * because it is a "/dev/fd/<fd>/.." path against an O_CLOEXEC fd, give 908c2ecf20Sopenharmony_ci * up now (on the assumption that the interpreter will want to load 918c2ecf20Sopenharmony_ci * this file). 928c2ecf20Sopenharmony_ci */ 938c2ecf20Sopenharmony_ci if (bprm->interp_flags & BINPRM_FLAGS_PATH_INACCESSIBLE) 948c2ecf20Sopenharmony_ci return -ENOENT; 958c2ecf20Sopenharmony_ci 968c2ecf20Sopenharmony_ci /* 978c2ecf20Sopenharmony_ci * OK, we've parsed out the interpreter name and 988c2ecf20Sopenharmony_ci * (optional) argument. 998c2ecf20Sopenharmony_ci * Splice in (1) the interpreter's name for argv[0] 1008c2ecf20Sopenharmony_ci * (2) (optional) argument to interpreter 1018c2ecf20Sopenharmony_ci * (3) filename of shell script (replace argv[0]) 1028c2ecf20Sopenharmony_ci * 1038c2ecf20Sopenharmony_ci * This is done in reverse order, because of how the 1048c2ecf20Sopenharmony_ci * user environment and arguments are stored. 1058c2ecf20Sopenharmony_ci */ 1068c2ecf20Sopenharmony_ci retval = remove_arg_zero(bprm); 1078c2ecf20Sopenharmony_ci if (retval) 1088c2ecf20Sopenharmony_ci return retval; 1098c2ecf20Sopenharmony_ci retval = copy_string_kernel(bprm->interp, bprm); 1108c2ecf20Sopenharmony_ci if (retval < 0) 1118c2ecf20Sopenharmony_ci return retval; 1128c2ecf20Sopenharmony_ci bprm->argc++; 1138c2ecf20Sopenharmony_ci *((char *)i_end) = '\0'; 1148c2ecf20Sopenharmony_ci if (i_arg) { 1158c2ecf20Sopenharmony_ci *((char *)i_sep) = '\0'; 1168c2ecf20Sopenharmony_ci retval = copy_string_kernel(i_arg, bprm); 1178c2ecf20Sopenharmony_ci if (retval < 0) 1188c2ecf20Sopenharmony_ci return retval; 1198c2ecf20Sopenharmony_ci bprm->argc++; 1208c2ecf20Sopenharmony_ci } 1218c2ecf20Sopenharmony_ci retval = copy_string_kernel(i_name, bprm); 1228c2ecf20Sopenharmony_ci if (retval) 1238c2ecf20Sopenharmony_ci return retval; 1248c2ecf20Sopenharmony_ci bprm->argc++; 1258c2ecf20Sopenharmony_ci retval = bprm_change_interp(i_name, bprm); 1268c2ecf20Sopenharmony_ci if (retval < 0) 1278c2ecf20Sopenharmony_ci return retval; 1288c2ecf20Sopenharmony_ci 1298c2ecf20Sopenharmony_ci /* 1308c2ecf20Sopenharmony_ci * OK, now restart the process with the interpreter's dentry. 1318c2ecf20Sopenharmony_ci */ 1328c2ecf20Sopenharmony_ci file = open_exec(i_name); 1338c2ecf20Sopenharmony_ci if (IS_ERR(file)) 1348c2ecf20Sopenharmony_ci return PTR_ERR(file); 1358c2ecf20Sopenharmony_ci 1368c2ecf20Sopenharmony_ci bprm->interpreter = file; 1378c2ecf20Sopenharmony_ci return 0; 1388c2ecf20Sopenharmony_ci} 1398c2ecf20Sopenharmony_ci 1408c2ecf20Sopenharmony_cistatic struct linux_binfmt script_format = { 1418c2ecf20Sopenharmony_ci .module = THIS_MODULE, 1428c2ecf20Sopenharmony_ci .load_binary = load_script, 1438c2ecf20Sopenharmony_ci}; 1448c2ecf20Sopenharmony_ci 1458c2ecf20Sopenharmony_cistatic int __init init_script_binfmt(void) 1468c2ecf20Sopenharmony_ci{ 1478c2ecf20Sopenharmony_ci register_binfmt(&script_format); 1488c2ecf20Sopenharmony_ci return 0; 1498c2ecf20Sopenharmony_ci} 1508c2ecf20Sopenharmony_ci 1518c2ecf20Sopenharmony_cistatic void __exit exit_script_binfmt(void) 1528c2ecf20Sopenharmony_ci{ 1538c2ecf20Sopenharmony_ci unregister_binfmt(&script_format); 1548c2ecf20Sopenharmony_ci} 1558c2ecf20Sopenharmony_ci 1568c2ecf20Sopenharmony_cicore_initcall(init_script_binfmt); 1578c2ecf20Sopenharmony_cimodule_exit(exit_script_binfmt); 1588c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL"); 159