Skip to contentSkip to conversation

nfsidmap: Purge the keyring when its full.

PATCH Superseded Steve Dickson 2 participants 3 messages Jan 12, 2012

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 nfsidmap: Purge the keyring when its full.

When a key can not be added to a keyring because the keyring is full, keyctl_instantiate() will fail with the errno being set to -EDQUOT. To recover, purge the keyring of all its keys and then try to add the new key.

---
 utils/nfsidmap/nfsidmap.c |   14 ++++++++++++--
 1 files changed, 12 insertions(+), 2 deletions(-)
utils/nfsidmap/nfsidmap.c +12 -2
index ce8cf3e..470f9d4 100644
@@ -3,6 +3,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <errno.h>
 
 #include <pwd.h>
 #include <grp.h>
@@ -25,6 +26,7 @@ char *usage="Usage: %s [-v] [-c || [-u|-g|-r key] || [-t timeout] key desc]";
 #define DEFAULT_KEYRING "id_resolver"
 #endif
 
+static int keyring_clear(char *keyring);
 
 #define UIDKEYS 0x1
 #define GIDKEYS 0x2
@@ -52,6 +54,14 @@ int id_lookup(char *name_at_domain, key_serial_t key, int type)
 
 	if (rc == 0) {
 		rc = keyctl_instantiate(key, id, strlen(id) + 1, 0);
+		if (rc < 0 && errno == -EDQUOT) {
+			/*
+			 * The keyring is full. Clear the keyring and try again
+			 */
+			rc = keyring_clear(DEFAULT_KEYRING);
+			if (rc == 0)
+				rc = keyctl_instantiate(key, id, strlen(id) + 1, 0);
+		}
 		if (rc < 0)
 			xlog_err("id_lookup: keyctl_instantiate failed: %m");
 	}
@@ -105,7 +115,6 @@ static int keyring_clear(char *keyring)
 	char buf[BUFSIZ];
 	key_serial_t key;
 
-	xlog_syslog(0);
 	if (keyring == NULL)
 		keyring = DEFAULT_KEYRING;
 
@@ -172,7 +181,7 @@ static int key_revoke(char *keystr, int keymask)
 		if ((keymask & mask) == 0)
 			continue;
 
-		if (strncmp(ptr+4, keystr, strlen(keystr)) != NULL)
+		if (strncmp(ptr+4, keystr, strlen(keystr)) != 0)
 			continue;
 
 		if (verbose) {
@@ -255,6 +264,7 @@ int main(int argc, char **argv)
 		return rc;		
 	}
 	if (clearing) {
+		xlog_syslog(0);
 		rc = keyring_clear(DEFAULT_KEYRING);
 		return rc;		
 	}
Signature · 1 line
1.7.7.5
#1 lore raw
Quoted 32 lines · Steve Dickson

When a key can not be added to a keyring because the keyring is full, keyctl_instantiate() will fail with the errno being set to -EDQUOT. To recover, purge the keyring of all its keys and then try to add the new key.

---
 utils/nfsidmap/nfsidmap.c |   14 ++++++++++++--
 1 files changed, 12 insertions(+), 2 deletions(-)
utils/nfsidmap/nfsidmap.c +2 -0
index ce8cf3e..470f9d4 100644
@@ -3,6 +3,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <errno.h>
 
 #include <pwd.h>
 #include <grp.h>
@@ -25,6 +26,7 @@ char *usage="Usage: %s [-v] [-c || [-u|-g|-r key] || [-t timeout] key desc]";
 #define DEFAULT_KEYRING "id_resolver"
 #endif
 
+static int keyring_clear(char *keyring);
 
 #define UIDKEYS 0x1
 #define GIDKEYS 0x2

utils/nfsidmap/nfsidmap.c int id_lookup(char *name_at_domain, key_serial_t key, int type) · line 57

Quoted 4 lines
utils/nfsidmap/nfsidmap.c +1 -0
@@ -52,6 +54,14 @@ int id_lookup(char *name_at_domain, key_serial_t key, int type)
 
 	if (rc == 0) {
 		rc = keyctl_instantiate(key, id, strlen(id) + 1, 0);
+		if (rc < 0 && errno == -EDQUOT) {

Shouldn't the above be a test for -ENFILE (or perhaps for both)?

Quoted 35 lines
utils/nfsidmap/nfsidmap.c +9 -2
+			/*
+			 * The keyring is full. Clear the keyring and try again
+			 */
+			rc = keyring_clear(DEFAULT_KEYRING);
+			if (rc == 0)
+				rc = keyctl_instantiate(key, id, strlen(id) + 1, 0);
+		}
 		if (rc < 0)
 			xlog_err("id_lookup: keyctl_instantiate failed: %m");
 	}
@@ -105,7 +115,6 @@ static int keyring_clear(char *keyring)
 	char buf[BUFSIZ];
 	key_serial_t key;
 
-	xlog_syslog(0);
 	if (keyring == NULL)
 		keyring = DEFAULT_KEYRING;
 
@@ -172,7 +181,7 @@ static int key_revoke(char *keystr, int keymask)
 		if ((keymask & mask) == 0)
 			continue;
 
-		if (strncmp(ptr+4, keystr, strlen(keystr)) != NULL)
+		if (strncmp(ptr+4, keystr, strlen(keystr)) != 0)
 			continue;
 
 		if (verbose) {
@@ -255,6 +264,7 @@ int main(int argc, char **argv)
 		return rc;		
 	}
 	if (clearing) {
+		xlog_syslog(0);
 		rc = keyring_clear(DEFAULT_KEYRING);
 		return rc;		
 	}
Signature · 6 lines
Trond Myklebust
Linux NFS client maintainer

NetApp
[email protected]
www.netapp.com
#2 lore raw
Quoted 34 lines · Trond Myklebust
Quoted 33 lines · Steve Dickson

When a key can not be added to a keyring because the keyring is full, keyctl_instantiate() will fail with the errno being set to -EDQUOT. To recover, purge the keyring of all its keys and then try to add the new key.

---
 utils/nfsidmap/nfsidmap.c |   14 ++++++++++++--
 1 files changed, 12 insertions(+), 2 deletions(-)
utils/nfsidmap/nfsidmap.c +2 -0
index ce8cf3e..470f9d4 100644
@@ -3,6 +3,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <errno.h>
 
 #include <pwd.h>
 #include <grp.h>
@@ -25,6 +26,7 @@ char *usage="Usage: %s [-v] [-c || [-u|-g|-r key] || [-t timeout] key desc]";
 #define DEFAULT_KEYRING "id_resolver"
 #endif
 
+static int keyring_clear(char *keyring);
 
 #define UIDKEYS 0x1
 #define GIDKEYS 0x2
@@ -52,6 +54,14 @@ int id_lookup(char *name_at_domain, key_serial_t key, int type)
 
Quoted 5 lines

utils/nfsidmap/nfsidmap.c int id_lookup(char *name_at_domain, key_serial_t key, int type) · line 57

Quoted 3 lines
utils/nfsidmap/nfsidmap.c +1 -0
 	if (rc == 0) {
 		rc = keyctl_instantiate(key, id, strlen(id) + 1, 0);
+		if (rc < 0 && errno == -EDQUOT) {

Shouldn't the above be a test for -ENFILE (or perhaps for both)?

Taking a look at the code, again, it appears -ENFILE is returned when there is no room for the new key. -EDQUOT and -ENOMEM are returned when there is no memory for the payload data.

We might as well purge the keyring when we get any of those error just to free up memory....

steved.

Quoted 37 lines
Quoted 36 lines
utils/nfsidmap/nfsidmap.c +9 -2
+			/*
+			 * The keyring is full. Clear the keyring and try again
+			 */
+			rc = keyring_clear(DEFAULT_KEYRING);
+			if (rc == 0)
+				rc = keyctl_instantiate(key, id, strlen(id) + 1, 0);
+		}
 		if (rc < 0)
 			xlog_err("id_lookup: keyctl_instantiate failed: %m");
 	}
@@ -105,7 +115,6 @@ static int keyring_clear(char *keyring)
 	char buf[BUFSIZ];
 	key_serial_t key;
 
-	xlog_syslog(0);
 	if (keyring == NULL)
 		keyring = DEFAULT_KEYRING;
 
@@ -172,7 +181,7 @@ static int key_revoke(char *keystr, int keymask)
 		if ((keymask & mask) == 0)
 			continue;
 
-		if (strncmp(ptr+4, keystr, strlen(keystr)) != NULL)
+		if (strncmp(ptr+4, keystr, strlen(keystr)) != 0)
 			continue;
 
 		if (verbose) {
@@ -255,6 +264,7 @@ int main(int argc, char **argv)
 		return rc;		
 	}
 	if (clearing) {
+		xlog_syslog(0);
 		rc = keyring_clear(DEFAULT_KEYRING);
 		return rc;		
 	}