Skip to contentSkip to conversation

apparmor: Convert to use match_string() helper

PATCH Superseded Andy Shevchenko 3 participants 3 messages May 7, 2018 – May 8, 2018

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 apparmor: Convert to use match_string() helper

The new helper returns index of the matching string in an array. We are going to use it here.

---
 security/apparmor/lsm.c | 24 ++++++++++--------------
 1 file changed, 10 insertions(+), 14 deletions(-)
security/apparmor/lsm.c +10 -14
index 033221d4fc6c..9d5006744356 100644
@@ -1392,14 +1392,12 @@ static int param_set_audit(const char *val, const struct kernel_param *kp)
 	if (apparmor_initialized && !policy_admin_capable(NULL))
 		return -EPERM;
 
-	for (i = 0; i < AUDIT_MAX_INDEX; i++) {
-		if (strcmp(val, audit_mode_names[i]) == 0) {
-			aa_g_audit = i;
-			return 0;
-		}
-	}
+	i = match_string(audit_mode_names, AUDIT_MAX_INDEX, val);
+	if (i < 0)
+		return -EINVAL;
 
-	return -EINVAL;
+	aa_g_audit = i;
+	return 0;
 }
 
 static int param_get_mode(char *buffer, const struct kernel_param *kp)
@@ -1423,14 +1421,12 @@ static int param_set_mode(const char *val, const struct kernel_param *kp)
 	if (apparmor_initialized && !policy_admin_capable(NULL))
 		return -EPERM;
 
-	for (i = 0; i < APPARMOR_MODE_NAMES_MAX_INDEX; i++) {
-		if (strcmp(val, aa_profile_mode_names[i]) == 0) {
-			aa_g_profile_mode = i;
-			return 0;
-		}
-	}
+	i = match_string(aa_profile_mode_names, APPARMOR_MODE_NAMES_MAX_INDEX, val);
+	if (i < 0)
+		return -EINVAL;
 
-	return -EINVAL;
+	aa_g_profile_mode = i;
+	return 0;
 }
 
 /*
Signature · 6 lines
2.17.0

--
To unsubscribe from this list: send the line "unsubscribe linux-security-module" in
the body of a message to majordomo at vger.kernel.org
More majordomo info@ http://vger.kernel.org/majordomo-info.html
#1 lore raw

PATCH 1/1 apparmor: Convert to use match_string() helper

return 0;

security/apparmor/lsm.c static int param_set_audit(const char *val, const struct kernel_param *kp) · line 1397

Quoted 5 lines
security/apparmor/lsm.c +3 -2
-		}
-	}
+	i = match_string(audit_mode_names, AUDIT_MAX_INDEX, val);
+	if (i < 0)
+		return -EINVAL;

Slight nitpick: according to the Linux kernel documentation, match_string() will return -EINVAL on failure.? You could probably just return 'i' if it's less than 0 and not explicitly return -EINVAL.

But good job replacing the code with a re-usable library call :-).

--
To unsubscribe from this list: send the line "unsubscribe linux-security-module" in the body of a message to majordomo at vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html

maintainer
#2 lore raw

PATCH 1/1 apparmor: Convert to use match_string() helper

In reply to Andy Shevchenko
Quoted 4 lines · Andy Shevchenko

The new helper returns index of the matching string in an array. We are going to use it here.

looks good thanks

I've pulled it into apparmor-next

Quoted 39 lines
---
 security/apparmor/lsm.c | 24 ++++++++++--------------
 1 file changed, 10 insertions(+), 14 deletions(-)
security/apparmor/lsm.c +5 -13
index 033221d4fc6c..9d5006744356 100644
@@ -1392,14 +1392,12 @@ static int param_set_audit(const char *val, const struct kernel_param *kp)
 	if (apparmor_initialized && !policy_admin_capable(NULL))
 		return -EPERM;
 
-	for (i = 0; i < AUDIT_MAX_INDEX; i++) {
-		if (strcmp(val, audit_mode_names[i]) == 0) {
-			aa_g_audit = i;
-			return 0;
-		}
-	}
+	i = match_string(audit_mode_names, AUDIT_MAX_INDEX, val);
+	if (i < 0)
+		return -EINVAL;
 
-	return -EINVAL;
+	aa_g_audit = i;
+	return 0;
 }
 
 static int param_get_mode(char *buffer, const struct kernel_param *kp)
@@ -1423,14 +1421,12 @@ static int param_set_mode(const char *val, const struct kernel_param *kp)
 	if (apparmor_initialized && !policy_admin_capable(NULL))
 		return -EPERM;
 
-	for (i = 0; i < APPARMOR_MODE_NAMES_MAX_INDEX; i++) {
-		if (strcmp(val, aa_profile_mode_names[i]) == 0) {
-			aa_g_profile_mode = i;
-			return 0;
-		}
-	}

security/apparmor/lsm.c static int param_set_mode(const char *val, const struct kernel_param *kp) · line 1432

Quoted 10 lines
security/apparmor/lsm.c +5 -1
+	i = match_string(aa_profile_mode_names, APPARMOR_MODE_NAMES_MAX_INDEX, val);
+	if (i < 0)
+		return -EINVAL;
 
-	return -EINVAL;
+	aa_g_profile_mode = i;
+	return 0;
 }
 
 /*

--
To unsubscribe from this list: send the line "unsubscribe linux-security-module" in the body of a message to majordomo at vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html