]> git.ipfire.org Git - thirdparty/tor.git/commitdiff
Have get_parent_directory() handle "/foo" and "/" correctly.
authorNick Mathewson <nickm@torproject.org>
Thu, 24 May 2012 16:56:31 +0000 (12:56 -0400)
committerNick Mathewson <nickm@torproject.org>
Thu, 24 May 2012 16:56:31 +0000 (12:56 -0400)
The parent of "/foo" is "/"; and "/" is its own parent.

This would cause Tor to fail if you tried to have a PF_UNIX control
socket in the root directory.  That would be a stupid thing to do
for other reasons, but there's no reason to fail like _this_.

Bug found by Esteban Manchado Velázquez. Fix for bug 5089; bugfix on
Tor 0.2.2.26-beta.  Unit test included.

changes/bug5089 [new file with mode: 0644]
src/common/compat.c
src/test/test_util.c

diff --git a/changes/bug5089 b/changes/bug5089
new file mode 100644 (file)
index 0000000..2062885
--- /dev/null
@@ -0,0 +1,5 @@
+  o Minor bugfixes:
+    - Correctly handle checking the permissions on the parent
+      directory of a control socket in the root directory. Bug found
+      by Esteban Manchado Velázquez. Fix for bug 5089; bugfix on Tor
+      0.2.2.26-beta.
index a4e50747cd514f7a78199c2639c5c0cab2983b06..ffd972482457fc45cc031945ab59048b3d8eb0f7 100644 (file)
@@ -1481,7 +1481,11 @@ get_user_homedir(const char *username)
 }
 #endif
 
-/** Modify <b>fname</b> to contain the name of the directory */
+/** Modify <b>fname</b> to contain the name of its parent directory.  Doesn't
+ * actually examine the filesystem; does a purely syntactic modification.
+ *
+ * The parent of the root director is considered to be iteself.
+ * */
 int
 get_parent_directory(char *fname)
 {
@@ -1503,13 +1507,18 @@ get_parent_directory(char *fname)
    */
   cp = fname + strlen(fname);
   at_end = 1;
-  while (--cp > fname) {
+  while (--cp >= fname) {
     int is_sep = (*cp == '/'
 #ifdef MS_WINDOWS
                   || *cp == '\\'
 #endif
                   );
     if (is_sep) {
+      if (cp == fname) {
+        /* This is the first separator in the file name; don't remove it! */
+        cp[1] = '\0';
+        return 0;
+      }
       *cp = '\0';
       if (! at_end)
         return 0;
index f9a83a38a5895cc9e3403510a7bd5c944c985e4c..1c7c2fec23757f6d59f2408e4fc67e010bf8b90c 100644 (file)
@@ -1299,11 +1299,12 @@ test_util_parent_dir(void *ptr)
 
   T("/home/wombat/knish", 0, "/home/wombat");
   T("/home/wombat/knish/", 0, "/home/wombat");
+  T("/home", 0, "/");
   T("./home/wombat/knish/", 0, "./home/wombat");
   T("./wombat", 0, ".");
   T("", -1, "");
-  T("/", -1, "");
-  T("////", -1, "");
+  T("/", 0, "/");
+  T("////", 0, "/");
 
  done:
   tor_free(cp);