1 2

[PATCH] Set PWD to readonly

Details
Message ID
<20190303161725.5076-1-sir@cmpwn.com>
DKIM signature
permerror
Download raw message
Patch: +5 -1
---
This is the first of several patches which will be necessary to make
$PWD POSIX-complaint.

>Assignments to this variable may be ignored. If an application sets or
>unsets the value of PWD, the behaviors of the cd and pwd utilities are
>unspecified.

An argument could be made for aborting the shell when PWD is set, or
tainting the shell and aborting later if cd/pwd is called again.

 shell/entry.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/shell/entry.c b/shell/entry.c
index 6b42d0c..d2f3c71 100644
--- a/shell/entry.c
+++ b/shell/entry.c
@@ -79,7 +79,11 @@ bool mrsh_populate_env(struct mrsh_state *state, char **environ) {
 			fprintf(stderr, "getcwd failed: %s\n", strerror(errno));
 			return false;
 		}
-		mrsh_env_set(state, "PWD", cwd, MRSH_VAR_ATTRIB_EXPORT);
+		mrsh_env_set(state, "PWD", cwd,
+				MRSH_VAR_ATTRIB_EXPORT | MRSH_VAR_ATTRIB_READONLY);
+	} else {
+		mrsh_env_set(state, "PWD", pwd,
+				MRSH_VAR_ATTRIB_EXPORT | MRSH_VAR_ATTRIB_READONLY);
 	}
 
 	mrsh_env_set(state, "OPTIND", "1", MRSH_VAR_ATTRIB_NONE);
-- 
-- 
2.21.0
Details
Message ID
<hpcBdp0QSwWfX8anLe0LIZYPZiWOWqgQtq9Z_s0dVwX8xwUQcCGU7R3acXNOEFCgPXJehzthgb4uEzV89E9r3i7Ubb2bPPlvD70lOgrenew=@emersion.fr>
In-Reply-To
<20190303161725.5076-1-sir@cmpwn.com> (view parent)
DKIM signature
permerror
Download raw message
On Sunday, March 3, 2019 5:17 PM, Drew DeVault <sir@cmpwn.com> wrote:
> This is the first of several patches which will be necessary to make
> $PWD POSIX-complaint.
>
> > Assignments to this variable may be ignored. If an application sets or
> > unsets the value of PWD, the behaviors of the cd and pwd utilities are
> > unspecified.
>
> An argument could be made for aborting the shell when PWD is set, or
> tainting the shell and aborting later if cd/pwd is called again.

"Unspecified behaviour" includes making PWD read-write and do not error
out when it's written to by the user. Thus, the old implementation
was POSIX-compliant in this regard. By the way, dash allows manually
setting PWD without any error.

However it is indeed better to provide an error message. I think making
the assignment fail is reasonable.

To git.sr.ht:~emersion/mrsh
   a2d4b7f..05fed9a  master -> master

Thanks!