summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Cashman <dcashman@google.com>2015-09-22 20:04:22 +0000
committerAndroid Git Automerger <android-git-automerger@android.com>2015-09-22 20:04:22 +0000
commit136dea0be37d07c2498453fdd4ae92ad871b8ae6 (patch)
treea047491a9b28d5c24cbb21cf8ec5623fd467dcde
parent7914896d2f4a46b469b4eed7bba0d698379ade8f (diff)
parentdeb18b511745d42d5c9dd0202db5e16b360577f7 (diff)
downloadlibselinux-136dea0be37d07c2498453fdd4ae92ad871b8ae6.tar.gz
am deb18b51: am 0feca1dd: am 87ceb1e2: Enable restorecon to properly label symlinks.
* commit 'deb18b511745d42d5c9dd0202db5e16b360577f7': Enable restorecon to properly label symlinks.
-rw-r--r--src/android.c47
1 files changed, 37 insertions, 10 deletions
diff --git a/src/android.c b/src/android.c
index 8f66a5a..f253954 100644
--- a/src/android.c
+++ b/src/android.c
@@ -31,6 +31,7 @@
#include <limits.h>
#include <sys/vfs.h>
#include <linux/magic.h>
+#include <libgen.h>
/*
* XXX Where should this configuration file be located?
@@ -1231,7 +1232,7 @@ static int selinux_android_restorecon_common(const char* pathname_orig,
struct statfs sfsb;
FTS *fts;
FTSENT *ftsent;
- char *pathname;
+ char *pathname = NULL, *pathdnamer = NULL, *pathdname, *pathbname;
char * paths[2] = { NULL , NULL };
int ftsflags = FTS_NOCHDIR | FTS_XDEV | FTS_PHYSICAL;
int error, sverrno;
@@ -1246,16 +1247,28 @@ static int selinux_android_restorecon_common(const char* pathname_orig,
if (!fc_sehandle)
return 0;
- // convert passed-in pathname to canonical pathname
- pathname = realpath(pathname_orig, NULL);
- if (!pathname) {
- sverrno = errno;
- selinux_log(SELINUX_ERROR, "SELinux: Could not get canonical path %s restorecon: %s.\n",
- pathname_orig, strerror(errno));
- errno = sverrno;
- error = -1;
- goto cleanup;
+ /*
+ * Convert passed-in pathname to canonical pathname by resolving realpath of
+ * containing dir, then appending last component name.
+ */
+ pathbname = basename(pathname_orig);
+ if (!strcmp(pathbname, "/") || !strcmp(pathbname, ".") || !strcmp(pathbname, "..")) {
+ pathname = realpath(pathname_orig, NULL);
+ if (!pathname)
+ goto realpatherr;
+ } else {
+ pathdname = dirname(pathname_orig);
+ pathdnamer = realpath(pathdname, NULL);
+ if (!pathdnamer)
+ goto realpatherr;
+ if (!strcmp(pathdnamer, "/"))
+ error = asprintf(&pathname, "/%s", pathbname);
+ else
+ error = asprintf(&pathname, "%s/%s", pathdnamer, pathbname);
+ if (error < 0)
+ goto oom;
}
+
paths[0] = pathname;
issys = (!strcmp(pathname, SYS_PATH)
|| !strncmp(pathname, SYS_PREFIX, sizeof(SYS_PREFIX)-1)) ? true : false;
@@ -1364,8 +1377,22 @@ out:
(void) fts_close(fts);
errno = sverrno;
cleanup:
+ free(pathdnamer);
free(pathname);
return error;
+oom:
+ sverrno = errno;
+ selinux_log(SELINUX_ERROR, "%s: Out of memory\n", __FUNCTION__);
+ errno = sverrno;
+ error = -1;
+ goto cleanup;
+realpatherr:
+ sverrno = errno;
+ selinux_log(SELINUX_ERROR, "SELinux: Could not get canonical path for %s restorecon: %s.\n",
+ pathname_orig, strerror(errno));
+ errno = sverrno;
+ error = -1;
+ goto cleanup;
}
int selinux_android_restorecon(const char *file, unsigned int flags)