I've been attempting to test some networking and had problems with
SELinux.getPeerContext when no context set using java.net.Socket - the app
aborts. This patch fixes the two problems in getPeerCon. I've also patched
the other get... methods/functions to set the initial pointer to null and
give the error.
Does this seem reasonable.
Richard
--- a/android_os_SELinux.cpp 2013-05-21 19:00:37.589485000 +0100
+++ b/android_os_SELinux.cpp 2013-05-21 19:45:40.208768298 +0100
@@ -102,16 +102,18 @@
return NULL;
}
- security_context_t tmp;
+ security_context_t tmp = NULL;
int ret = getpeercon(fd, &tmp);
Unique_SecurityContext context(tmp);
ScopedLocalRef<jstring> contextStr(env, NULL);
if (ret != -1) {
contextStr.reset(env->NewStringUTF(context.get()));
+ } else {
+ ALOGE("getPeerCon error: %s (errno %d)", strerror(errno), errno);
}
- ALOGV("getPeerCon(%d) => %s", fd, contextStr.get());
+ ALOGV("getPeerCon(%d) => %s", fd, context.get());
return contextStr.release();
}
@@ -198,13 +200,15 @@
return NULL;
}
- security_context_t tmp;
+ security_context_t tmp = NULL;
int ret = getfilecon(path.c_str(), &tmp);
Unique_SecurityContext context(tmp);
ScopedLocalRef<jstring> securityString(env, NULL);
if (ret != -1) {
securityString.reset(env->NewStringUTF(context.get()));
+ } else {
+ ALOGE("getFileCon error: %s (errno %d)", strerror(errno), errno);
}
ALOGV("getFileCon(%s) => %s", path.c_str(), context.get());
@@ -224,13 +228,15 @@
return NULL;
}
- security_context_t tmp;
+ security_context_t tmp = NULL;
int ret = getcon(&tmp);
Unique_SecurityContext context(tmp);
ScopedLocalRef<jstring> securityString(env, NULL);
if (ret != -1) {
securityString.reset(env->NewStringUTF(context.get()));
+ } else {
+ ALOGE("getCon error: %s (errno %d)", strerror(errno), errno);
}
ALOGV("getCon() => %s", context.get());
@@ -251,13 +257,15 @@
return NULL;
}
- security_context_t tmp;
+ security_context_t tmp = NULL;
int ret = getpidcon(static_cast<pid_t>(pid), &tmp);
Unique_SecurityContext context(tmp);
ScopedLocalRef<jstring> securityString(env, NULL);
if (ret != -1) {
securityString.reset(env->NewStringUTF(context.get()));
+ } else {
+ ALOGE("getPidCon error: %s (errno %d)", strerror(errno), errno);
}
ALOGV("getPidCon(%d) => %s", pid, context.get());