From dfe1f2c108431fe58450cb08e3545e016c0e5400 Mon Sep 17 00:00:00 2001 From: Shaka Huang Date: Sat, 3 Apr 2021 16:08:36 +0800 Subject: [PATCH] Call freecon() when fgetfilecon() succeeds --- native/jni/core/restorecon.cpp | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/native/jni/core/restorecon.cpp b/native/jni/core/restorecon.cpp index a819e55b0..f4454dfd1 100644 --- a/native/jni/core/restorecon.cpp +++ b/native/jni/core/restorecon.cpp @@ -18,10 +18,11 @@ static void restore_syscon(int dirfd) { DIR *dir; char *con; - fgetfilecon(dirfd, &con); - if (strlen(con) == 0 || strcmp(con, UNLABEL_CON) == 0) - fsetfilecon(dirfd, SYSTEM_CON); - freecon(con); + if (fgetfilecon(dirfd, &con) >= 0) { + if (strlen(con) == 0 || strcmp(con, UNLABEL_CON) == 0) + fsetfilecon(dirfd, SYSTEM_CON); + freecon(con); + } dir = xfdopendir(dirfd); while ((entry = xreaddir(dir))) { @@ -29,10 +30,11 @@ static void restore_syscon(int dirfd) { if (entry->d_type == DT_DIR) { restore_syscon(fd); } else if (entry->d_type == DT_REG) { - fgetfilecon(fd, &con); - if (con[0] == '\0' || strcmp(con, UNLABEL_CON) == 0) - fsetfilecon(fd, SYSTEM_CON); - freecon(con); + if (fgetfilecon(fd, &con) >= 0) { + if (con[0] == '\0' || strcmp(con, UNLABEL_CON) == 0) + fsetfilecon(fd, SYSTEM_CON); + freecon(con); + } } else if (entry->d_type == DT_LNK) { getfilecon_at(dirfd, entry->d_name, &con); if (con[0] == '\0' || strcmp(con, UNLABEL_CON) == 0)