shithub: 9pro

Download patch

ref: 996dd23001bb1f21e1fbf324bce4016baa5d85c4
parent: 8b670ed42a11f902584582651e24aeccf8667405
author: Sigrid Haflínudóttir <ftrvxmtrx@gmail.com>
date: Thu Jun 18 08:41:39 EDT 2020

9pex: add -e option to allow escaping root

--- a/9pex.c
+++ b/9pex.c
@@ -95,7 +95,7 @@
 
 static int in, out, eof;
 static C9ctx ctx;
-static int debug;
+static int debug, rootescape;
 static Fid **fids;
 static int numfids;
 static Tag **tags;
@@ -382,13 +382,18 @@
 		memmove(path+plen+1, el[i], ellen);
 		path[plen+1+ellen] = 0;
 
-		if ((real = realpath(path, NULL)) == NULL)
-			break;
-		free(path);
-		if (strlen(real) < rootlen) { /* don't escape root */
-			free(real);
-			real = strdup(rootpath);
+		if (!rootescape) {
+			if ((real = realpath(path, NULL)) == NULL)
+				break;
+			free(path);
+			if (strlen(real) < rootlen) { /* don't escape root */
+				free(real);
+				real = strdup(rootpath);
+			}
+		} else {
+			real = path;
 		}
+
 		free(p);
 		p = real;
 
@@ -870,7 +875,7 @@
 
 	debug = 0;
 	dir = NULL;
-	while ((c = parg_getopt(&ps, argc, argv, "dh")) >= 0) {
+	while ((c = parg_getopt(&ps, argc, argv, "deh")) >= 0) {
 		switch (c) {
 		case 1:
 			if (dir != NULL) {
@@ -879,11 +884,14 @@
 			}
 			dir = ps.optarg;
 			break;
+		case 'e':
+			rootescape++;
+			break;
 		case 'd':
 			debug++;
 			break;
 		case 'h':
-			fprintf(stderr, "usage: 9pex [-d] DIR\n");
+			fprintf(stderr, "usage: 9pex [-e] [-d] DIR\n");
 			return 0;
 			break;
 		case '?':
--- a/README.md
+++ b/README.md
@@ -20,5 +20,6 @@
 
 # Notes/todo
 
+ * 9pex: to allow "escaping the root" by following symlinks you have to pass `-e` option
  * 9pex: chrooting with musl makes `realpath` not work as it requires /proc to be mounted, get rid of `realpath`?
  * 9gc: you can run it in termux on Android like so: `rlwrap -a'XXXXXXwth' -s 0 -t dumb 9gc -e NICKNAME`