From c29cf5ec2a79ef40eb09dcec3ae7402142850838 Mon Sep 17 00:00:00 2001 From: Matheus Afonso Martins Moreira Date: Mon, 13 May 2024 07:37:26 -0300 Subject: [PATCH 1/2] feat: support BASH_SOURCE_PATH * shell: restrict BASH_SOURCE_PATH when appropriate Make the BASH_SOURCE_PATH variable read-only and unsettable when the shell is operating in restricted mode. This variable should be restricted for the same reasons why PATH is restricted. Modifications: * When we are not in the POSIX mode, we consider BASH_SOURCE_PATH if present even without the new option `-i'. * The new variable BASH_SOURCE_PATH has been disabled at all in the restricted shell since this would introduce a new loophole in existing setups of the restricted shells. If the new variable were enabled in the restricted shells, all the existing setups would need to be updated to care about the variable BASH_SOURCE_PATH, which would be an unreasonable requirement. In addition, considering the purpose of the restricted shells, we would not need to provide the new feature to the restricted shells. Co-authored-by: Koichi Murase --- builtins/source.def | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/builtins/source.def b/builtins/source.def index 51dddec6..4498ac9e 100644 --- a/builtins/source.def +++ b/builtins/source.def @@ -116,7 +116,7 @@ int source_builtin (WORD_LIST *list) { int result; - char *filename, *debug_trap, *x; + char *filename, *source_path, *debug_trap, *x; if (no_options (list)) return (EX_USAGE); @@ -143,20 +143,15 @@ source_builtin (WORD_LIST *list) filename = savestring (list->word->word); else if (absolute_pathname (list->word->word)) filename = savestring (list->word->word); - else if (source_uses_path) - { -#if 0 - char *spath; + else if ( + !posixly_correct && #if defined (RESTRICTED_SHELL) - if (restricted == 0 && posixly_correct == 0 && (spath = path_value ("BASH_SOURCE_PATH", 1))) -#else - if (posixly_correct == 0 && (spath = path_value ("BASH_SOURCE_PATH", 1))) -#endif - filename = find_in_path (list->word->word, spath, FS_READABLE); - else + !restricted && #endif - filename = find_path_file (list->word->word); - } + (source_path = path_value ("BASH_SOURCE_PATH", 1)) != NULL) + filename = find_in_path (list->word->word, source_path, FS_READABLE); + if (filename == 0 && source_uses_path) + filename = find_path_file (list->word->word); if (filename == 0) { if (source_searches_cwd == 0) -- 2.43.0