Associative array & Massive expansion of keyring capacity
Subsystems (6)
David Howells
maintainercover Associative array & Massive expansion of keyring capacity
Here's a set of patches that do two main things:
(1) Provide a general purpose associative array implementation.
(2) Use the associative array implementation to provide key pointer storage for keyrings thereby massively increasing capacity.
There are numerous smaller patches also that are split out of (2) to make the patch in (2) smaller, plus a fix patch that didn't get taken into the last merge window.
The patches can also be found at:
http://git.kernel.org/cgit/linux/kernel/git/dhowells/linux-fs.git/log/?h=keys-devel
The impetus for these is that the NFS ID mapper uses keyrings to store name to ID mappings, but is running up against the limitation imposed by the flat array currently used by the keyring.
The associative array implementation does all the work. To quickly summarise its capabilities: no capacity limit; objects don't need to be modified to go in it (no list_head/rb_node equivalent needed); objects need a unique index key of arbitrary length; objects can be anything; modifications are generally RCU-safe; the contents can be iterated over and it can directly walk to an object in O(log(N)-ish) time given the key; and lastly, there are several features to reduce the memory footprint (node sharing and shortcuts). Documentation is included in the patch.
Note that the associative array implementation was developed in userspace and has been thoroughly valground and tested inserting new objects whilst iterating over 2,000,000 objects amongst other things.
The keyutils testsuite has been run successfully against a kernel with these patches applied, though the testsuite did need some tweaks: (a) previously keys added to a keyring with no removals appeared to be ordered and (b) some of the errors have changed.
David
---
David Howells (10):
KEYS: Skip key state checks when checking for possession Add a generic associative array implementation.
KEYS: Use bool in make_key_ref() and is_key_possessed()
KEYS: key_is_dead() should take a const key pointer argument
KEYS: Consolidate the concept of an 'index key' for key access
KEYS: Introduce a search context structure
KEYS: Search for auth-key by name rather than targt key ID
KEYS: Define a __key_get() wrapper to use rather than atomic_inc()
KEYS: Drop the permissions argument from __keyring_search_one()
KEYS: Expand the capacity of a keyring
Documentation/assoc_array.txt | 583 +++++++++++++ Documentation/security/keys.txt | 20 include/keys/keyring-type.h | 17 include/linux/assoc_array.h | 94 ++ include/linux/assoc_array_priv.h | 159 +++ include/linux/key-type.h | 5 include/linux/key.h | 48 + lib/Kconfig | 14 lib/Makefile | 1 lib/assoc_array.c | 1734 ++++++++++++++++++++++++++++++++++++++ security/keys/Kconfig | 1 security/keys/gc.c | 30 - security/keys/internal.h | 65 + security/keys/key.c | 91 +- security/keys/keyring.c | 1447 ++++++++++++++++---------------- security/keys/proc.c | 17 security/keys/process_keys.c | 131 +-- security/keys/request_key.c | 56 + security/keys/request_key_auth.c | 31 - security/keys/user_defined.c | 18 20 files changed, 3596 insertions(+), 966 deletions(-) create mode 100644 Documentation/assoc_array.txt create mode 100644 include/linux/assoc_array.h create mode 100644 include/linux/assoc_array_priv.h create mode 100644 lib/assoc_array.c
David Howells
maintainerPATCH 1/10 KEYS: Skip key state checks when checking for possession
Skip key state checks (invalidation, revocation and expiration) when checking for possession. Without this, keys that have been marked invalid, revoked keys and expired keys are not given a possession attribute - which means the possessor is not granted any possession permits and cannot do anything with them unless they also have one a user, group or other permit.
This causes failures in the keyutils test suite's revocation and expiration tests now that commit 96b5c8fea6c0861621051290d705ec2e971963f1 reduced the initial permissions granted to a key.
The failures are due to accesses to revoked and expired keys being given EACCES instead of EKEYREVOKED or EKEYEXPIRED.
---
security/keys/internal.h | 1 + security/keys/process_keys.c | 8 +++++--- security/keys/request_key.c | 6 ++++-- security/keys/request_key_auth.c | 2 +- 4 files changed, 11 insertions(+), 6 deletions(-)
security/keys/internal.h +1 -0
@@ -124,6 +124,7 @@ extern key_ref_t search_my_process_keyrings(struct key_type *type,extern key_ref_t search_process_keyrings (struct key_type *type,const void *description,key_match_func_t match, +bool no_state_check,const struct cred *cred);extern struct key *find_keyring_by_name (const char *name,bool skip_perm_check);
security/keys/process_keys.c +5 -3
@@ -440,6 +440,7 @@ found:key_ref_t search_process_keyrings (struct key_type *type,const void *description,key_match_func_t match, +bool no_state_check,const struct cred *cred) {struct request_key_auth *rka;
@@ -448,7 +449,7 @@ key_ref_t search_process_keyrings(struct key_type *type,might_sleep (); key_ref =search_my_process_keyrings (type, description, match, - false, cred); + no_state_check, cred);if (!IS_ERR (key_ref)) goto found; err = key_ref;
@@ -468,7 +469,8 @@ key_ref_t search_process_keyrings(struct key_type *type, rka = cred->request_key_auth->payload.data; key_ref =search_process_keyrings (type, description, - match, rka->cred); + match, no_state_check, + rka->cred);up_read (&cred->request_key_auth->sem);
@@ -675,7 +677,7 @@ try_again:/* check to see if we possess the key */ skey_ref =search_process_keyrings (key->type, key, lookup_user_key_possessed, - cred); + true, cred);if (!IS_ERR (skey_ref)) {key_put (key);
security/keys/request_key.c +4 -2
@@ -390,7 +390,8 @@ static int construct_alloc_key(struct key_type *type,* waited for locks */ mutex_lock (&key_construction_mutex); - key_ref =search_process_keyrings (type, description, type->match, cred); + key_ref =search_process_keyrings (type, description, type->match, + false, cred);if (!IS_ERR (key_ref)) goto key_already_present;
@@ -539,7 +540,8 @@ struct key *request_key_and_link(struct key_type *type, dest_keyring, flags);/* search all the process keyrings for a key */ - key_ref =search_process_keyrings (type, description, type->match, cred); + key_ref =search_process_keyrings (type, description, type->match, + false, cred);if (!IS_ERR (key_ref)) { key =key_ref_to_ptr (key_ref);
security/keys/request_key_auth.c +1 -1
@@ -247,7 +247,7 @@ struct key *key_get_instantiation_authkey(key_serial_t target_id) &key_type_request_key_auth, (void *) (unsigned long ) target_id, key_get_instantiation_authkey_match, - cred); + false, cred);if (IS_ERR (authkey_ref)) { authkey =ERR_CAST (authkey_ref);
David Howells
maintainerPATCH 2/10 Add a generic associative array implementation.
In reply to David HowellsAdd a generic associative array implementation that can be used as the container for keyrings, thereby massively increasing the capacity available whilst also speeding up searching in keyrings that contain a lot of keys.
This may also be useful in FS-Cache for tracking cookies.
Documentation is added into Documentation/associative_array.txt
Some of the properties of the implementation are:
(1) Objects are opaque pointers. The implementation does not care where they point (if anywhere) or what they point to (if anything).
[!] NOTE: Pointers to objects _must_ be zero in the two least significant
bits.
(2) Objects do not need to contain linkage blocks for use by the array. This permits an object to be located in multiple arrays simultaneously. Rather, the array is made up of metadata blocks that point to objects.
(3) Objects are labelled as being one of two types (the type is a bool value). This information is stored in the array, but has no consequence to the array itself or its algorithms.
(4) Objects require index keys to locate them within the array.
(5) Index keys must be unique. Inserting an object with the same key as one already in the array will replace the old object.
(6) Index keys can be of any length and can be of different lengths.
(7) Index keys should encode the length early on, before any variation due to length is seen.
(8) Index keys can include a hash to scatter objects throughout the array.
(9) The array can iterated over. The objects will not necessarily come out in key order.
(10) The array can be iterated whilst it is being modified, provided the RCU readlock is being held by the iterator. Note, however, under these circumstances, some objects may be seen more than once. If this is a problem, the iterator should lock against modification. Objects will not be missed, however, unless deleted.
(11) Objects in the array can be looked up by means of their index key.
(12) Objects can be looked up whilst the array is being modified, provided the RCU readlock is being held by the thread doing the look up.
The implementation uses a tree of 16-pointer nodes internally that are indexed on each level by nibbles from the index key. To improve memory efficiency, shortcuts can be emplaced to skip over what would otherwise be a series of single-occupancy nodes. Further, nodes pack leaf object pointers into spare space in the node rather than making an extra branch until as such time an object needs to be added to a full node.
---
Documentation/assoc_array.txt | 583 +++++++++++++ include/linux/assoc_array.h | 94 ++ include/linux/assoc_array_priv.h | 159 +++ lib/Kconfig | 14 lib/Makefile | 1 lib/assoc_array.c | 1734 ++++++++++++++++++++++++++++++++++++++ 6 files changed, 2585 insertions(+) create mode 100644 Documentation/assoc_array.txt create mode 100644 include/linux/assoc_array.h create mode 100644 include/linux/assoc_array_priv.h create mode 100644 lib/assoc_array.c
Documentation/assoc_array.txt new file +583 -0
@@ -0,0 +1,583 @@ + ======================================== + GENERIC ASSOCIATIVE ARRAY IMPLEMENTATION + ======================================== + +Contents: + + - Overview. + + - The public API. + - Edit script. + - Operations table. + - Manipulation functions. + - Access functions. + - Index key form. + + - Internal workings. + - Basic internal tree layout. + - Shortcuts. + - Splitting and collapsing nodes. + - Non-recursive iteration. + - Simultaneous alteration and iteration. + + +======== +OVERVIEW +======== + +This associative array implementation is an object container with the following +properties: + + (1) Objects are opaque pointers. The implementation does not care where they + point (if anywhere) or what they point to (if anything). + + [!] NOTE: Pointers to objects _must_ be zero in the two least significant + bits. + + (2) Objects do not need to contain linkage blocks for use by the array. This + permits an object to be located in multiple arrays simultaneously. + Rather, the array is made up of metadata blocks that point to objects. + + (3) Objects are labelled as being one of two types (the type is a bool value). + This information is stored in the array, but has no consequence to the + array itself or its algorithms. + + (4) Objects require index keys to locate them within the array. + + (5) Index keys must be unique. Inserting an object with the same key as one + already in the array will replace the old object. + + (6) Index keys can be of any length and can be of different lengths. + + (7) Index keys should encode the length early on, before any variation due to + length is seen. + + (8) Index keys can include a hash to scatter objects throughout the array. + + (9) The array can iterated over. The objects will not necessarily come out in + key order. + +(10) The array can be iterated over whilst it is being modified, provided the + RCU readlock is being held by the iterator. Note, however, under these + circumstances, some objects may be seen more than once. If this is a + problem, the iterator should lock against modification. Objects will not + be missed, however, unless deleted. + +(11) Objects in the array can be looked up by means of their index key. + +(12) Objects can be looked up whilst the array is being modified, provided the + RCU readlock is being held by the thread doing the look up. + +The implementation uses a tree of 16-pointer nodes internally that are indexed +on each level by nibbles from the index key. To improve memory efficiency, +shortcuts can be emplaced to skip over what would otherwise be a series of +single-occupancy nodes. Further, nodes pack leaf object pointers into spare +space in the node rather than making an extra branch until as such time an +object needs to be added to a full node. + + +============== +THE PUBLIC API +============== + +The public API can be found in <linux/assoc_array.h>. The associative array is +rooted on the following structure: + + struct assoc_array { + ... + }; + +The code is selected by enabling CONFIG_ASSOCIATIVE_ARRAY. + + +EDIT SCRIPT +----------- + +The insertion and deletion functions produce an 'edit script' that can later be +applied to effect the changes without risking ENOMEM. This retains the +preallocated metadata blocks that will be installed in the internal tree and +keeps track of the metadata blocks that will be removed from the tree when the +script is applied. + +This is also used to keep track of dead blocks and dead objects after the +script has been applied so that they can be freed later. The freeing is done +after an RCU grace period has passed - thus allowing access functions to +proceed under the RCU read lock. + +The script appears as outside of the API as a pointer of the type: + + struct assoc_array_edit; + +There are two functions for dealing with the script: + + (1) Apply an edit script. + + void assoc_array_apply_edit(struct assoc_array_edit *edit); + + This will perform the edit functions, interpolating various write barriers + to permit accesses under the RCU read lock to continue. The edit script + will then be passed to call_rcu() to free it and any dead stuff it points + to. + + (2) Cancel an edit script. + + void assoc_array_cancel_edit(struct assoc_array_edit *edit); + + This frees the edit script and all preallocated memory immediately. If + this was for insertion, the new object is _not_ released by this function, + but must rather be released by the caller. + +These functions are guaranteed not to fail. + + +OPERATIONS TABLE +---------------- + +Various functions take a table of operations: + + struct assoc_array_ops { + ... + }; + +This points to a number of methods, all of which need to be provided: + + (1) Get a chunk of index key from caller data: + + unsigned long (*get_key_chunk)(const void *index_key, int level); + + This should return a chunk of caller-supplied index key starting at the + *bit* position given by the level argument. The level argument will be a + multiple of ASSOC_ARRAY_KEY_CHUNK_SIZE and the function should return + ASSOC_ARRAY_KEY_CHUNK_SIZE bits. No error is possible. + + + (2) Get a chunk of an object's index key. + + unsigned long (*get_object_key_chunk)(const void *object, int level); + + As the previous function, but gets its data from an object in the array + rather than from a caller-supplied index key. + + + (3) See if this is the object we're looking for. + + bool (*compare_object)(const void *object, const void *index_key); + + Compare the object against an index key and return true if it matches and + false if it doesn't. + + + (4) Diff the index keys of two objects. + + int (*diff_objects)(const void *a, const void *b); + + Return the bit position at which the index keys of two objects differ or + -1 if they are the same. + + + (5) Free an object. + + void (*free_object)(void *object); + + Free the specified object. Note that this may be called an RCU grace + period after assoc_array_apply_edit() was called, so synchronize_rcu() may + be necessary on module unloading. + + +MANIPULATION FUNCTIONS +---------------------- + +There are a number of functions for manipulating an associative array: + + (1) Initialise an associative array. + + void assoc_array_init(struct assoc_array *array); + + This initialises the base structure for an associative array. It can't + fail. + + + (2) Insert/replace an object in an associative array. + + struct assoc_array_edit * + assoc_array_insert(struct assoc_array *array, + const struct assoc_array_ops *ops, + const void *index_key, + void *object, bool type); + + This inserts the given object into the array. Note that the least + significant two bits of the pointer must be zero as they're used to + type-mark pointers internally. The type argument is an arbitrary + two-value type retained by the array but not used by the algorithms. + + If an object already exists for that key then it will be replaced with the + new object and the old one will be freed automatically. + + The index_key argument should hold index key information and is + passed to the methods in the ops table when they are called. + + This function makes no alteration to the array itself, but rather returns + an edit script that must be applied. -ENOMEM is returned in the case of + an out-of-memory error. + + The caller should lock exclusively against other modifiers of the array. + + + (3) Delete an object from an associative array. + + struct assoc_array_edit * + assoc_array_delete(struct assoc_array *array, + const struct assoc_array_ops *ops, + const void *index_key); + + This deletes an object that matches the specified data from the array. + + The index_key argument should hold index key information and is + passed to the methods in the ops table when they are called. + + This function makes no alteration to the array itself, but rather returns + an edit script that must be applied. -ENOMEM is returned in the case of + an out-of-memory error. NULL will be returned if the specified object is + not found within the array. + + The caller should lock exclusively against other modifiers of the array. + + + (4) Delete all objects from an associative array. + + struct assoc_array_edit * + assoc_array_clear(struct assoc_array *array, + const struct assoc_array_ops *ops); + + This deletes all the objects from an associative array and leaves it + completely empty. + + This function makes no alteration to the array itself, but rather returns + an edit script that must be applied. -ENOMEM is returned in the case of + an out-of-memory error. + + The caller should lock exclusively against other modifiers of the array. + + + (5) Destroy an associative array, deleting all objects. + + void assoc_array_destroy(struct assoc_array *array, + const struct assoc_array_ops *ops); + + This destroys the contents of the associative array and leaves it + completely empty. It is not permitted for another thread to be traversing + the array under the RCU read lock at the same time as this function is + destroying it as no RCU deferral is performed on memory release - + something that would require memory to be allocated. + + The caller should lock exclusively against other modifiers and accessors + of the array. + + + (6) Garbage collect an associative array. + + int assoc_array_gc(struct assoc_array *array, + const struct assoc_array_ops *ops, + bool (*iterator)(void *object, bool type, + void *iterator_data), + void *iterator_data); + + This iterates over the objects in an associative array and passes each one + to iterator(). If iterator() returns true, the object is kept. If it + returns false, the object will be freed. If the iterator() function + returns true, it must perform any appropriate refcount incrementing on the + object before returning. + + The internal tree will be packed down if possible as part of the iteration + to reduce the number of nodes in it. + + The iterator_data is passed directly to iterator() and is otherwise + ignored by the function. + + The function will return 0 if successful and -ENOMEM if there wasn't + enough memory. + + It is possible for other threads to iterate over or search the array under + the RCU read lock whilst this function is in progress. The caller should + lock exclusively against other modifiers of the array. + + +ACCESS FUNCTIONS +---------------- + +There are two functions for accessing an associative array: + + (1) Iterate over all the objects in an associative array. + + int assoc_array_iterate(const struct assoc_array *array, + int (*iterator)(const void *object, bool type, + void *iterator_data), + void *iterator_data); + + This passes each object in the array to the iterator callback function. + iterator_data is private data for that function. + + This may be used on an array at the same time as the array is being + modified, provided the RCU read lock is held. Under such circumstances, + it is possible for the iteration function to see some objects twice. If + this is a problem, then modification should be locked against. The + iteration algorithm should not, however, miss any objects. + + The function will return 0 if no objects were in the array or else it will + return the result of the last iterator function called. Iteration stops + immediately if any call to the iteration function results in a non-zero + return. + + + (2) Find an object in an associative array. + + void *assoc_array_find(const struct assoc_array *array, + const struct assoc_array_ops *ops, + const void *index_key, + bool *_type); + + This walks through the array's internal tree directly to the object + specified by the index key. If the object is present then a pointer to it + is returned and the type is stored in *_type. + + This may be used on an array at the same time as the array is being + modified, provided the RCU read lock is held. + + The function will return the object if found (and set *_type to the object + type) or will return NULL if the object was not found. + + +INDEX KEY FORM +-------------- + +The index key can be of any form, but since the algorithms aren't told how long +the key is, it is strongly recommended that the index key includes its length +very early on before any variation due to the length would have an effect on +comparisons. + +This will cause leaves with different length keys to scatter away from each +other - and those with the same length keys to cluster together. + +It is also recommended that the index key begin with a hash of the rest of the +key to maximise scattering throughout keyspace. + +The better the scattering, the wider and lower the internal tree will be. + +Poor scattering isn't too much of a problem as there are shortcuts and nodes +can contain mixtures of leaves and metadata pointers. + +The index key is read in chunks of machine word. Each chunk is subdivided into +one nibble (4 bits) per level, so on a 32-bit CPU this is good for 8 levels and +on a 64-bit CPU, 16 levels. Unless the scattering is really poor, it is +unlikely that more than one word of any particular index key will have to be +used. + + +================= +INTERNAL WORKINGS +================= + +The associative array data structure has an internal tree. This tree is +constructed of two types of metadata blocks: nodes and shortcuts. + +A node is an array of slots. Each slot can contain one of four things: + + (*) A NULL pointer, indicating that the slot is empty. + + (*) A pointer to one of two types of object (leaves). + + (*) A pointer to a node at the next level. + + (*) A pointer to a shortcut. + + +BASIC INTERNAL TREE LAYOUT +-------------------------- + +Ignoring shortcuts for the moment, the nodes form a multilevel tree. The index +key space is strictly subdivided by the nodes in the tree and nodes occur on +fixed levels. For example: + + Level: 0 1 2 3 + =============== =============== =============== =============== + NODE D + NODE B NODE C +------>+---+ + +------>+---+ +------>+---+ | | 0 | + NODE A | | 0 | | | 0 | | +---+ + +---+ | +---+ | +---+ | : : + | 0 | | : : | : : | +---+ + +---+ | +---+ | +---+ | | f | + | 1 |---+ | 3 |---+ | 7 |---+ +---+ + +---+ +---+ +---+ + : : : : | 8 |---+ + +---+ +---+ +---+ | NODE E + | e |---+ | f | : : +------>+---+ + +---+ | +---+ +---+ | 0 | + | f | | | f | +---+ + +---+ | +---+ : : + | NODE F +---+ + +------>+---+ | f | + | 0 | NODE G +---+ + +---+ +------>+---+ + : : | | 0 | + +---+ | +---+ + | 6 |---+ : : + +---+ +---+ + : : | f | + +---+ +---+ + | f | + +---+ + +In the above example, there are 7 nodes (A-G), each with 16 slots (0-f). +Assuming no other meta data nodes in the tree, the key space is divided thusly: + + KEY PREFIX NODE + ========== ==== + 137* D + 138* E + 13[0-69-f]* C + 1[0-24-f]* B + e3* G + e[0-57-f]* F + [02-df]* A + +So, for instance, keys with the following example index keys will be found in +the appropriate nodes: + + INDEX KEY PREFIX NODE + =============== ======= ==== + 13694892892489 13 C + 13795289025897 137 D + 13889dde88793 138 E + 138bbb89003093 138 E + 1394879524789 12 C + 1458952489 1 B + 9431809de993ba - A + b4542910809cd - A + e5284310def98 e F + e68428974237 e6 G + e7fffcbd443 e F + f3842239082 - A + +To save memory, if a node can hold all the leaves in its portion of keyspace, +then the node will have all those leaves in it and will not have any metadata +pointers - even if some of those leaves would like to be in the same slot. + +A node can contain a heterogeneous mix of leaves and metadata pointers. +Metadata pointers must be in the slots that match their subdivisions of key +space. The leaves can be in any slot not occupied by a metadata pointer. It +is guaranteed that none of the leaves in a node will match a slot occupied by a +metadata pointer. If the metadata pointer is there, any leaf whose key matches +the metadata key prefix must be in the subtree that the metadata pointer points +to. + +In the above example list of index keys, node A will contain: + + SLOT CONTENT INDEX KEY (PREFIX) + ==== =============== ================== + 1 PTR TO NODE B 1* + any LEAF 9431809de993ba + any LEAF b4542910809cd + e PTR TO NODE F e* + any LEAF f3842239082 + +and node B: + + 3 PTR TO NODE C 13* + any LEAF 1458952489 + + +SHORTCUTS +--------- + +Shortcuts are metadata records that jump over a piece of keyspace. A shortcut +is a replacement for a series of single-occupancy nodes ascending through the +levels. Shortcuts exist to save memory and to speed up traversal. + +It is possible for the root of the tree to be a shortcut - say, for example, +the tree contains at least 17 nodes all with key prefix '1111'. The insertion +algorithm will insert a shortcut to skip over the '1111' keyspace in a single +bound and get to the fourth level where these actually become different. + + +SPLITTING AND COLLAPSING NODES +------------------------------ + +Each node has a maximum capacity of 16 leaves and metadata pointers. If the +insertion algorithm finds that it is trying to insert a 17th object into a +node, that node will be split such that at least two leaves that have a common +key segment at that level end up in a separate node rooted on that slot for +that common key segment. + +If the leaves in a full node and the leaf that is being inserted are +sufficiently similar, then a shortcut will be inserted into the tree. + +When the number of objects in the subtree rooted at a node falls to 16 or +fewer, then the subtree will be collapsed down to a single node - and this will +ripple towards the root if possible. + + +NON-RECURSIVE ITERATION +----------------------- + +Each node and shortcut contains a back pointer to its parent and the number of +slot in that parent that points to it. None-recursive iteration uses these to +proceed rootwards through the tree, going to the parent node, slot N + 1 to +make sure progress is made without the need for a stack. + +The backpointers, however, make simultaneous alteration and iteration tricky. + + +SIMULTANEOUS ALTERATION AND ITERATION +------------------------------------- + +There are a number of cases to consider: + + (1) Simple insert/replace. This involves simply replacing a NULL or old + matching leaf pointer with the pointer to the new leaf after a barrier. + The metadata blocks don't change otherwise. An old leaf won't be freed + until after the RCU grace period. + + (2) Simple delete. This involves just clearing an old matching leaf. The + metadata blocks don't change otherwise. The old leaf won't be freed until + after the RCU grace period. + + (3) Insertion replacing part of a subtree that we haven't yet entered. This + may involve replacement of part of that subtree - but that won't affect + the iteration as we won't have reached the pointer to it yet and the + ancestry blocks are not replaced (the layout of those does not change). + + (4) Insertion replacing nodes that we're actively processing. This isn't a + problem as we've passed the anchoring pointer and won't switch onto the + new layout until we follow the back pointers - at which point we've + already examined the leaves in the replaced node (we iterate over all the + leaves in a node before following any of its metadata pointers). + + We might, however, re-see some leaves that have been split out into a new + branch that's in a slot further along than we were at. + + (5) Insertion replacing nodes that we're processing a dependent branch of. + This won't affect us until we follow the back pointers. Similar to (4). + + (6) Deletion collapsing a branch under us. This doesn't affect us because the + back pointers will get us back to the parent of the new node before we + could see the new node. The entire collapsed subtree is thrown away + unchanged - and will still be rooted on the same slot, so we shouldn't + process it a second time as we'll go back to slot + 1. + +Note: + + (*) Under some circumstances, we need to simultaneously change the parent + pointer and the parent slot pointer on a node (say, for example, we + inserted another node before it and moved it up a level). We cannot do + this without locking against a read - so we have to replace that node too. + + However, when we're changing a shortcut into a node this isn't a problem + as shortcuts only have one slot and so the parent slot number isn't used + when traversing backwards over one. This means that it's okay to change + the slot number first - provided suitable barriers are used to make sure + the parent slot number is read after the back pointer. + +Obsolete blocks and leaves are freed up after an RCU grace period has passed, +so as long as anyone doing walking or iteration holds the RCU read lock, the +old superstructure should not go away on them.
include/linux/assoc_array.h new file +94 -0
94 lines not on this page · raw message
include/linux/assoc_array_priv.h new file +159 -0
159 lines not on this page · raw message
lib/Kconfig +14 -0
@@ -312,6 +312,20 @@ config TEXTSEARCH_FSM config BTREE boolean +config ASSOCIATIVE_ARRAY + bool + help + Generic associative array. Can be searched and iterated over whilst + it is being modified. It is also reasonably quick to search and + modify. The algorithms are non-recursive, and the trees are highly + capacious. + + See: + + Documentation/assoc_array.txt + + for more information. + config HAS_IOMEM boolean depends on !NO_IOMEM
lib/Makefile +1 -0
@@ -49,6 +49,7 @@ CFLAGS_hweight.o = $(subst $(quote),,$(CONFIG_ARCH_HWEIGHT_CFLAGS)) obj-$(CONFIG_GENERIC_HWEIGHT) += hweight.o obj-$(CONFIG_BTREE) += btree.o +obj-$(CONFIG_ASSOCIATIVE_ARRAY) += assoc_array.o obj-$(CONFIG_DEBUG_PREEMPT) += smp_processor_id.o obj-$(CONFIG_DEBUG_LIST) += list_debug.o obj-$(CONFIG_DEBUG_OBJECTS) += debugobjects.o
lib/assoc_array.c new file +1734 -0
1,734 lines not on this page · raw message
Re: [PATCH 02/10] Add a generic associative array implementation.
include/linux/assoc_array.h line 67
Quoted 8 lines · David Howells
Add a generic associative array implementation that can be used as the container for keyrings, thereby massively increasing the capacity available whilst also speeding up searching in keyrings that contain a lot of keys.
include/linux/assoc_array.h new file +4
-0+extern int assoc_array_iterate (const struct assoc_array *array, +int (*iterator)(const void *object,bool type, +void *iterator_data), +void *iterator_data);
bool type makes me queasy.
David Howells
maintainerRe: [PATCH 02/10] Add a generic associative array implementation.
Quoted 1 line · Joe Perches
bool type makes me queasy.
:-)
I've no particular objection to naming it something else - but what? 'Mark' maybe?
David
David Howells
maintainerPATCH 1/1 Assoc_array: Drop leaf-type concept
In reply to Joe PerchesHi Joe, George,
How about the attached changes? I've dropped support for multiple leaf types in the assoc_array API (leaving that to the caller). The array still uses bit 0 of the object pointer to mark an internal metadata pointer if set (and then uses bit 1 to specify the subtype internally).
It makes assoc_array.o about 250 bytes smaller too, whereas keyrings.o grows by about 150 - so a net gain.
David
--- Documentation/assoc_array.txt | 45 +++++++------------ include/linux/assoc_array.h | 10 +--- include/linux/assoc_array_priv.h | 85 ++++++++++++++++++++++-------------- lib/assoc_array.c | 87 +++++++++++++++++------------------- security/keys/gc.c | 2 security/keys/internal.h | 2 security/keys/keyring.c | 92 +++++++++++++++++++++++++-------------- 7 files changed, 179 insertions(+), 144 deletions(-)
Documentation/assoc_array.txt +18 -27
@@ -31,41 +31,36 @@ properties: (1) Objects are opaque pointers. The implementation does not care where they point (if anywhere) or what they point to (if anything). - [!] NOTE: Pointers to objects _must_ be zero in the two least significant - bits. + [!] NOTE: Pointers to objects _must_ be zero in the least significant bit. (2) Objects do not need to contain linkage blocks for use by the array. This permits an object to be located in multiple arrays simultaneously. Rather, the array is made up of metadata blocks that point to objects. - (3) Objects are labelled as being one of two types (the type is a bool value). - This information is stored in the array, but has no consequence to the - array itself or its algorithms. + (3) Objects require index keys to locate them within the array. - (4) Objects require index keys to locate them within the array. - - (5) Index keys must be unique. Inserting an object with the same key as one + (4) Index keys must be unique. Inserting an object with the same key as one already in the array will replace the old object. - (6) Index keys can be of any length and can be of different lengths. + (5) Index keys can be of any length and can be of different lengths. - (7) Index keys should encode the length early on, before any variation due to + (6) Index keys should encode the length early on, before any variation due to length is seen. - (8) Index keys can include a hash to scatter objects throughout the array. + (7) Index keys can include a hash to scatter objects throughout the array. - (9) The array can iterated over. The objects will not necessarily come out in + (8) The array can iterated over. The objects will not necessarily come out in key order. -(10) The array can be iterated over whilst it is being modified, provided the + (9) The array can be iterated over whilst it is being modified, provided the RCU readlock is being held by the iterator. Note, however, under these circumstances, some objects may be seen more than once. If this is a problem, the iterator should lock against modification. Objects will not be missed, however, unless deleted. -(11) Objects in the array can be looked up by means of their index key. +(10) Objects in the array can be looked up by means of their index key. -(12) Objects can be looked up whilst the array is being modified, provided the +(11) Objects can be looked up whilst the array is being modified, provided the RCU readlock is being held by the thread doing the look up. The implementation uses a tree of 16-pointer nodes internally that are indexed
@@ -203,12 +198,11 @@ There are a number of functions for manipulating an associative array: assoc_array_insert(struct assoc_array *array, const struct assoc_array_ops *ops, const void *index_key, - void *object, bool type); + void *object); This inserts the given object into the array. Note that the least - significant two bits of the pointer must be zero as they're used to - type-mark pointers internally. The type argument is an arbitrary - two-value type retained by the array but not used by the algorithms. + significant bit of the pointer must be zero as it's used to type-mark + pointers internally. If an object already exists for that key then it will be replaced with the new object and the old one will be freed automatically.
@@ -278,8 +272,7 @@ There are a number of functions for manipulating an associative array: int assoc_array_gc(struct assoc_array *array, const struct assoc_array_ops *ops, - bool (*iterator)(void *object, bool type, - void *iterator_data), + bool (*iterator)(void *object, void *iterator_data), void *iterator_data); This iterates over the objects in an associative array and passes each one
@@ -310,7 +303,7 @@ There are two functions for accessing an associative array: (1) Iterate over all the objects in an associative array. int assoc_array_iterate(const struct assoc_array *array, - int (*iterator)(const void *object, bool type, + int (*iterator)(const void *object, void *iterator_data), void *iterator_data);
@@ -333,12 +326,10 @@ There are two functions for accessing an associative array: void *assoc_array_find(const struct assoc_array *array, const struct assoc_array_ops *ops, - const void *index_key, - bool *_type); + const void *index_key); This walks through the array's internal tree directly to the object - specified by the index key. If the object is present then a pointer to it - is returned and the type is stored in *_type. + specified by the index key.. This may be used on an array at the same time as the array is being modified, provided the RCU read lock is held.
@@ -384,7 +375,7 @@ A node is an array of slots. Each slot can contain one of four things: (*) A NULL pointer, indicating that the slot is empty. - (*) A pointer to one of two types of object (leaves). + (*) A pointer to an object (a leaf). (*) A pointer to a node at the next level.
include/linux/assoc_array.h +4 -6
@@ -62,19 +62,18 @@ static inline void assoc_array_init(struct assoc_array *array) }extern int assoc_array_iterate (const struct assoc_array *array, -int (*iterator)(const void *object,bool type, +int (*iterator)(const void *object,void *iterator_data),void *iterator_data);extern void *assoc_array_find (const struct assoc_array *array,const struct assoc_array_ops *ops, -const void *index_key, -bool *_type); +const void *index_key);extern void assoc_array_destroy (struct assoc_array *array,const struct assoc_array_ops *ops);extern struct assoc_array_edit *assoc_array_insert (struct assoc_array *array,const struct assoc_array_ops *ops,const void *index_key, -void *object,bool type); +void *object);extern void assoc_array_insert_set_object (struct assoc_array_edit *edit,void *object); extern struct assoc_array_edit *assoc_array_delete(struct assoc_array *array,
@@ -86,8 +85,7 @@ extern void assoc_array_apply_edit(struct assoc_array_edit *edit);extern void assoc_array_cancel_edit (struct assoc_array_edit *edit);extern int assoc_array_gc (struct assoc_array *array,const struct assoc_array_ops *ops, -bool (*iterator)(void *object,bool type, -void *iterator_data), +bool (*iterator)(void *object,void *iterator_data),void *iterator_data);#endif /* CONFIG_ASSOCIATIVE_ARRAY */
include/linux/assoc_array_priv.h +54 -31
122 lines not on this page · raw message
lib/assoc_array.c +41 -46
247 lines not on this page · raw message
security/keys/gc.c +1 -1
@@ -130,7 +130,7 @@ void key_gc_keytype(struct key_type *ktype)kleave ("" ); } -static int key_gc_keyring_func (const void *object,bool type,void *iterator_data) +static int key_gc_keyring_func (const void *object,void *iterator_data) {const struct key *key = object;time_t *limit = iterator_data;
security/keys/internal.h +1 -1
@@ -122,7 +122,7 @@ struct keyring_search_context {#define KEYRING_SEARCH_NO_CHECK_PERM 0x0010/* Don't check permissions */ #define KEYRING_SEARCH_DETECT_TOO_DEEP 0x0020/* Give an error on excessive depth */ -int (*iterator)(const void *object,bool type,void *iterator_data); +int (*iterator)(const void *object,void *iterator_data);/* Internal stuff */ int skipped_ret;
security/keys/keyring.c +60 -32
219 lines not on this page · raw message
Re: [PATCH] Assoc_array: Drop leaf-type concept
Looks cleaner.
The larger issues that bother me:
1) The fact that the abstract daya type "index key" provides access to a byte string "index key" is a bit confusing. I'd describe it as
follows: (Based on my current understanding.)
Each object has an associated "index key" that is used for lookup. The index key is a string of bits which is stored "inside" the object, however all access to it is done through method functions, so it is not necessary to explicitly store it anywhere.
For example, it is possible to have the leading portion of the index_key be a hash of the remainder.
Index keys may be variable-length, but must be self-delimiting. Specifically, two different index keys must differ at some bit position before the end of the shorter of the two.
Some index key methods take an isolated "const void *index_key" argument, while others take an object pointer.
2) The code does not look 32-bit safe. In particular, keyring_get_key_chunk assumes the hash is BITS_PER_LONG long, but keyring_diff_objects assumes it's 8*8 bits...
3) I presume you looked at wider tries like Judy arrays and found that a fan-out of 16 was better?
4) There are 3/7 unused bytes in an assoc_array_node, after parent_slot. Would it be worth storing 16/48 bits of index key and 3/4 bits of number of levels to skip per assoc_array_node?
Since parent_slot is actually only 4 bits, you could store 4 bits of nubmer of levels and 56 bits (up to 14 levels, plus the one from the famout) of index key.
With 32-way fanout, you could fit 5 bits of parent index, 25/55 bits of index key, and encode the number of levels to skip (0-5/11) more carefully.
5) The big question: Why not use a hash table? That's what people usually expect when you say "associative array". It would be simpler and faster.
RCU-safe resizeable hash tables are tricky, but a solved problem, There are universal hashing techniques for preventing hash collision attacks.
David Howells
maintainerRe: [PATCH] Assoc_array: Drop leaf-type concept
Quoted 1 line · George Spelvin
Looks cleaner.
Thanks!
Quoted 12 lines
The larger issues that bother me:
1) The fact that the abstract daya type "index key" provides access to a byte string "index key" is a bit confusing. I'd describe it as
follows: (Based on my current understanding.)Each object has an associated "index key" that is used for lookup. The index key is a string of bits which is stored "inside" the object, however all access to it is done through method functions, so it is not necessary to explicitly store it anywhere.
Quoted 6 lines
For example, it is possible to have the leading portion of the
index_keybe a hash of the remainder.Index keys may be variable-length, but must be self-delimiting. Specifically, two different index keys must differ at some bit position before the end of the shorter of the two.
This is a reasonable summary - but there's no requirement for the index key to be a string of bits in its native form. It's just that it must be presented to my routines as a bit string. It doesn't even need to be stored inside the object - though I did note you quoted that. Maybe "associated with"?
Quoted 2 lines
Some index key methods take an isolated "const void
*index_key" argument, while others take an object pointer.
Indeed. Sometimes I have an explicit index key available (look up, for example) and sometimes I only have the object available (determining whether a key I'm inserting matches a key I already have when debating shortcut creation).
The reason assoc_array_insert() takes an explicit index key _and_ an object (which provides an implicit index key) is that it permits me to use assoc_array_walk() - which only wants the the explicit index key.
Quoted 3 lines
2) The code does not look 32-bit safe. In particular,
keyring_get_key_chunkassumes the hash isBITS_PER_LONGlong, butkeyring_diff_objectsassumes it's 8*8 bits...
Good point. Fixed.
Quoted 2 lines
3) I presume you looked at wider tries like Judy arrays and found that a fan-out of 16 was better?
I have not looked at Judy arrays, but I have looked at the other stuff Linux has available and some stuff beyond that.
To select the algorithm, I started off with a list of criteria:
(1) It must not be necessary to modify an object to be able to include it in the container. That means no list_head, rb_node or similar emplacement within the object.
(2) Can't use nodes > PAGE_SIZE. Preferably they'd be much smaller.
(3) Having lots of blocks that have one object pointer and two or three metadata pointers is bad. I'd prefer a much higher object-to-meta pointer ratio.
(4) All algorithms must be non-recursive functions as stack space is at a premium. Also can't rely on being able to allocate extra space during R/O accesses.
(5) Must be possible for RCU R/O accesses to be concurrent with modification.
(6) Reading must be non-sleeping (so no use of a semaphore for reading).
(7) Concurrent modification is not a requirement.
(8) Needs to be primarily optimised for direct walking to a node by index key.
(9) Needs reasonable iteration performance too.
(10) Modifications shouldn't have to replace a lot of internal blocks.
I think the biggest pain is actually the non-recursive algorithm criterion.
I have actually used this container before for a disk-based tree.
Anyway, with regards to the size of the fan out, I wanted a compromise between having slack available so as not to get too deep a tree too quickly whilst not have too much slack space - after all, kernel memory is not easily reclaimable.
However, the biggest factor was that I decided to grab the index key a chunk at a time. The logical choice for chunk size was register size (ie. unsigned long) and ideally there would be no wastage in the chunk I got back. This means a fan out that divides exactly in to 2^32 and 2^64. From that point of view, the choices are 2^(N^2). A fan out of 16 seems nice as it's a nibble or one hex digit.
Another factor was that I wanted, if possible, to have keyring objects to cluster together to make iterating over them easier (and I know when to stop iterating over them because the rest of the objects are just keys), but still have them fan out to make them easier to look up directly. The direct lookup is of lesser importance because keyrings tend not to point to many other keyrings.
Quoted 9 lines
4) There are 3/7 unused bytes in an
assoc_array_node, afterparent_slot. Would it be worth storing 16/48 bits of index key and 3/4 bits of number of levels to skip perassoc_array_node?
Sinceparent_slotis actually only 4 bits, you could store 4 bits of nubmer of levels and 56 bits (up to 14 levels, plus the one from the famout) of index key.With 32-way fanout, you could fit 5 bits of parent index, 25/55 bits of index key, and encode the number of levels to skip (0-5/11) more carefully.
You mean merge some shortcut representation into a node? Maybe. It gets messy, though, if the parent of such a node gets folded into *its* parent and leaves the "skip+node" out of range of the index key storage that it has. Now you have to replace the "skip node" too (or insert a shortcut). Multiply by the fanout...
Actually, I'd rather store the index key nibble for each object I have a pointer too. That would take up another word sadly (16 * log2(16)), but would improve insertion and searching a bit.
Quoted 3 lines
5) The big question: Why not use a hash table? That's what people usually expect when you say "associative array". It would be simpler and faster.
What I've implemented *is* in essence a series of nested hash tables;-)
But, in answer, no, it wouldn't necessarily be faster, but yes, it would mostly be simpler[*]. The hash table approach would also be less memory efficient generally - though one can construct datasets that would be horribly inefficient for both algorithms - and much less efficient for mine.
[*] Until it comes to expanding or collapsing the hash table. How do you work
out how much to expand it or contract it by to get a good spread of objects?
A hash table would not be able to grow beyond PAGE_SIZE practically[**] - so a maximum of 512 buckets on most 64-bit systems (assuming no other per-bucket metadata for things like occupancy counts). However, I'm looking at a case with a need to store ~36000 keys in a keyring - so on the order of 70 keys per hash bucket. Assuming a perfect distribution, my array would require about 2500 nodes in a tree four levels deep and the maximum number of index-key-to-object comparisons you'd have to make would be 16.
[**] order>0 allocations are to be avoided where possible, but it would be
possible to stitch multiple pages together with a higher level pointer block - which adds complexity.
In a hash table the hash function is much more critical as it's only applied once and you may not have control over variety of data you're going to be asked to hash. Also, in the keyrings case, you cannot link the objects (keys) directly into the hash table, so each object would need to be represented by a linkage block.
Further, once you've got a few objects into a hash table, a traditional hash table is actually slower and less efficient because the object pointers are spread over more cachelines.
I will grant you, though, that hash table look up, iteration, insertion and deletion are all simpler - the last two _provided_ you do not need to resize the hash table.
But I won't grant you that a hash table would necessarily be faster for the given amount of metadata memory used.
Quoted 1 line
RCU-safe resizeable hash tables are tricky, but a solved problem,
Conceptually, that's not too hard - provided you are willing to accept a complete replacement of the entire metadata tree. You have to do that so as not to destabilise iteration and lookup when items jump to a different bucket.
Remember: modification shalt not block iteration or lookup.
Quoted 2 lines
There are universal hashing techniques for preventing hash collision attacks.
I'm not too worried about attacks. If someone really wants to attack, you can't really stop them. The main difficulty with hashing is that you might have no control over the data you're hashing. The upside is that there's no reason you have to only use one hashing function. In my case, hashing functions could be per-keytype for example.
David
David Howells
maintainerPATCH 3/10 KEYS: Use bool in make_key_ref() and is_key_possessed()
In reply to David HowellsMake make_key_ref() take a bool possession parameter and make is_key_possessed() return a bool.
---
Documentation/security/keys.txt | 7 +++---- include/linux/key.h | 4 ++-- security/keys/keyring.c | 5 +++-- 3 files changed, 8 insertions(+), 8 deletions(-)
Documentation/security/keys.txt +3 -4
@@ -865,15 +865,14 @@ encountered: calling processes has a searchable link to the key from one of its keyrings. There are three functions for dealing with these: - key_ref_t make_key_ref(const struct key *key, - unsigned long possession); + key_ref_t make_key_ref(const struct key *key, bool possession); struct key *key_ref_to_ptr(const key_ref_t key_ref); - unsigned long is_key_possessed(const key_ref_t key_ref); + bool is_key_possessed(const key_ref_t key_ref); The first function constructs a key reference from a key pointer and - possession information (which must be 0 or 1 and not any other value). + possession information (which must be true or false). The second function retrieves the key pointer from a reference and the third retrieves the possession flag.
include/linux/key.h +2 -2
@@ -99,7 +99,7 @@ struct keyring_name;typedef struct __key_reference_with_attributes *key_ref_t ;static inline key_ref_t make_key_ref (const struct key *key, -unsigned long possession) +bool possession) {return (key_ref_t) ((unsigned long ) key | possession); }
@@ -109,7 +109,7 @@ static inline struct key *key_ref_to_ptr(const key_ref_t key_ref)return (struct key *) ((unsigned long ) key_ref & ~1UL ); } -static inline unsigned long is_key_possessed (const key_ref_t key_ref) +static inline bool is_key_possessed (const key_ref_t key_ref) {return (unsigned long ) key_ref &1UL ; }
security/keys/keyring.c +3 -2
@@ -329,9 +329,10 @@ key_ref_t keyring_search_aux(key_ref_t keyring_ref,struct keyring_list *keylist;struct timespec now; -unsigned long possessed, kflags; +unsigned long kflags;struct key *keyring, *key;key_ref_t key_ref; +bool possessed;long err;int sp, nkeys, kix;
@@ -542,8 +543,8 @@ key_ref_t __keyring_search_one(key_ref_t keyring_ref, key_perm_t perm) {struct keyring_list *klist; -unsigned long possessed;struct key *keyring, *key; +bool possessed;int nkeys, loop; keyring =key_ref_to_ptr (keyring_ref);
David Howells
maintainerPATCH 4/10 KEYS: key_is_dead() should take a const key pointer argument
In reply to David Howellskey_is_dead() should take a const key pointer argument as it doesn't modify what it points to.
---
security/keys/internal.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
security/keys/internal.h +1 -1
@@ -203,7 +203,7 @@ extern struct key *key_get_instantiation_authkey(key_serial_t target_id);/* * Determine whether a key is dead. */ -static inline bool key_is_dead (struct key *key,time_t limit) +static inline bool key_is_dead (const struct key *key,time_t limit) {return key->flags & ((1 << KEY_FLAG_DEAD) |
David Howells
maintainerPATCH 5/10 KEYS: Consolidate the concept of an 'index key' for key access
In reply to David HowellsConsolidate the concept of an 'index key' for accessing keys. The index key is the search term needed to find a key directly - basically the key type and the key description. We can add to that the description length.
This will be useful when turning a keyring into an associative array rather than just a pointer block.
---
include/linux/key.h | 21 ++++++++++--- security/keys/internal.h | 8 ++--- security/keys/key.c | 72 ++++++++++++++++++++++--------------------- security/keys/keyring.c | 37 +++++++++++----------- security/keys/request_key.c | 12 +++++-- 5 files changed, 83 insertions(+), 67 deletions(-)
include/linux/key.h +16 -5
@@ -82,6 +82,12 @@ struct key_owner;struct keyring_list ;struct keyring_name ; +struct keyring_index_key { +struct key_type *type; +const char *description; +size_t desc_len; +}; +/*****************************************************************************/ /* * key reference with possession attribute handling
@@ -129,7 +135,6 @@ struct key {struct list_head graveyard_link;struct rb_node serial_node; }; -struct key_type *type;/* type of key */ struct rw_semaphore sem;/* change vs change sem */ struct key_user *user;/* owner of this key */ void *security;/* security data for this key */
@@ -163,12 +168,18 @@ struct key {#define KEY_FLAG_ROOT_CAN_CLEAR 6/* set if key can be cleared by root without permission */ #define KEY_FLAG_INVALIDATED 7/* set if key has been invalidated */ -/* the description string -* - this is used to match a key against search criteria -* - this should be a printable string +/* the key type and key description string +* - the desc is used to match a key against search criteria +* - it should be a printable string * - eg: for krb5 AFS, this might be "[email protected]" */ -char *description; +union { +struct keyring_index_key index_key; +struct { +struct key_type *type;/* type of key */ +char *description; + }; + };/* type specific data * - this is used by the keyring type to index the name
security/keys/internal.h +3 -5
@@ -89,19 +89,17 @@ extern struct key_type *key_type_lookup(const char *type);extern void key_type_put (struct key_type *ktype);extern int __key_link_begin (struct key *keyring, -const struct key_type *type, -const char *description, +const struct keyring_index_key *index_key,unsigned long *_prealloc);extern int __key_link_check_live_key (struct key *keyring,struct key *key);extern void __key_link (struct key *keyring,struct key *key,unsigned long *_prealloc);extern void __key_link_end (struct key *keyring, -struct key_type *type, +const struct keyring_index_key *index_key,unsigned long prealloc);extern key_ref_t __keyring_search_one (key_ref_t keyring_ref, -const struct key_type *type, -const char *description, +const struct keyring_index_key *index_key,key_perm_t perm); extern struct key *keyring_search_instkey(struct key *keyring,
security/keys/key.c +37 -35
179 lines not on this page · raw message
security/keys/keyring.c +19 -18
@@ -538,8 +538,7 @@ EXPORT_SYMBOL(keyring_search);* to the returned key reference. */ key_ref_t __keyring_search_one (key_ref_t keyring_ref, -const struct key_type *ktype, -const char *description, +const struct keyring_index_key *index_key,key_perm_t perm) {struct keyring_list *klist;
@@ -558,9 +557,9 @@ key_ref_t __keyring_search_one(key_ref_t keyring_ref,smp_rmb ();for (loop =0 ; loop < nkeys ; loop++) { key =rcu_dereference (klist->keys[loop]); - if (key->type == ktype && + if (key->type == index_key->type && (!key->type->match || - key->type->match(key, description)) && + key->type->match(key, index_key->description)) && key_permission(make_key_ref(key, possessed), perm) == 0 && !(key->flags & ((1 << KEY_FLAG_INVALIDATED) |
@@ -747,8 +746,8 @@ static void keyring_unlink_rcu_disposal(struct rcu_head *rcu)/* * Preallocate memory so that a key can be linked into to a keyring. */ -int __key_link_begin (struct key *keyring,const struct key_type *type, -const char *description,unsigned long *_prealloc) +int __key_link_begin (struct key *keyring,const struct keyring_index_key *index_key, +unsigned long *_prealloc) __acquires(&keyring->sem) __acquires(&keyring_serialise_link_sem) {
@@ -759,7 +758,8 @@ int __key_link_begin(struct key *keyring, const struct key_type *type,size_t size;int loop, lru, ret; -kenter ("%d,%s,%s," ,key_serial (keyring), type->name, description); +kenter ("%d,%s,%s," , +key_serial (keyring), index_key->type->name, index_key->description);if (keyring->type != &key_type_keyring)return -ENOTDIR ;
@@ -772,7 +772,7 @@ int __key_link_begin(struct key *keyring, const struct key_type *type,/* serialise link/link calls to prevent parallel calls causing a cycle * when linking two keyring in opposite orders */ -if (type == &key_type_keyring) +if (index_key->type == &key_type_keyring)down_write (&keyring_serialise_link_sem); klist =rcu_dereference_locked_keyring (keyring);
@@ -784,8 +784,8 @@ int __key_link_begin(struct key *keyring, const struct key_type *type,for (loop = klist->nkeys -1 ; loop >=0 ; loop--) {struct key *key =rcu_deref_link_locked (klist, loop, keyring); -if (key->type == type && -strcmp (key->description, description) ==0 ) { +if (key->type == index_key->type && +strcmp (key->description, index_key->description) ==0 ) {/* Found a match - we'll replace the link with * one to the new key. We record the slot * position.
@@ -865,7 +865,7 @@ error_quota:key_payload_reserve (keyring, keyring->datalen -KEYQUOTA_LINK_BYTES ); error_sem: -if (type == &key_type_keyring) +if (index_key->type == &key_type_keyring)up_write (&keyring_serialise_link_sem); error_krsem:up_write (&keyring->sem);
@@ -957,16 +957,17 @@ void __key_link(struct key *keyring, struct key *key,* * Must be called with __key_link_begin() having being called. */ -void __key_link_end (struct key *keyring,struct key_type *type, +void __key_link_end (struct key *keyring, +const struct keyring_index_key *index_key,unsigned long prealloc) __releases(&keyring->sem) __releases(&keyring_serialise_link_sem) { -BUG_ON (type ==NULL ); -BUG_ON (type->name ==NULL ); -kenter ("%d,%s,%lx" , keyring->serial, type->name, prealloc); +BUG_ON (index_key->type ==NULL ); +BUG_ON (index_key->type->name ==NULL ); +kenter ("%d,%s,%lx" , keyring->serial, index_key->type->name, prealloc); -if (type == &key_type_keyring) +if (index_key->type == &key_type_keyring)up_write (&keyring_serialise_link_sem);if (prealloc) {
@@ -1007,12 +1008,12 @@ int key_link(struct key *keyring, struct key *key)key_check (keyring);key_check (key); - ret =__key_link_begin (keyring, key->type, key->description, &prealloc); + ret =__key_link_begin (keyring, &key->index_key, &prealloc);if (ret ==0 ) { ret =__key_link_check_live_key (keyring, key);if (ret ==0 )__key_link (keyring, key, &prealloc); -__key_link_end (keyring, key->type, prealloc); +__key_link_end (keyring, &key->index_key, prealloc); }return ret;
security/keys/request_key.c +8 -4
@@ -352,6 +352,11 @@ static int construct_alloc_key(struct key_type *type,struct key_user *user, struct key **_key) { + const struct keyring_index_key index_key = { + .type = type, + .description = description, + .desc_len =strlen (description), + };const struct cred *cred =current_cred ();unsigned long prealloc;struct key *key;
@@ -379,8 +384,7 @@ static int construct_alloc_key(struct key_type *type,set_bit (KEY_FLAG_USER_CONSTRUCT , &key->flags);if (dest_keyring) { - ret =__key_link_begin (dest_keyring, type, description, - &prealloc); + ret =__key_link_begin (dest_keyring, &index_key, &prealloc);if (ret <0 ) goto link_prealloc_failed; }
@@ -400,7 +404,7 @@ static int construct_alloc_key(struct key_type *type,mutex_unlock (&key_construction_mutex);if (dest_keyring) -__key_link_end (dest_keyring, type, prealloc); +__key_link_end (dest_keyring, &index_key, prealloc);mutex_unlock (&user->cons_lock); *_key = key;kleave (" = 0 [%d]" ,key_serial (key));
@@ -416,7 +420,7 @@ key_already_present: ret =__key_link_check_live_key (dest_keyring, key);if (ret ==0 )__key_link (dest_keyring, key, &prealloc); -__key_link_end (dest_keyring, type, prealloc); +__key_link_end (dest_keyring, &index_key, prealloc);if (ret <0 ) goto link_check_failed; }
David Howells
maintainerPATCH 6/10 KEYS: Introduce a search context structure
In reply to David HowellsSearch functions pass around a bunch of arguments, each of which gets copied with each call. Introduce a search context structure to hold these.
Whilst we're at it, create a search flag that indicates whether the search should be directly to the description or whether it should iterate through all keys looking for a non-description match.
This will be useful when keyrings use a generic data struct with generic routines to manage their content as the search terms can just be passed through to the iterator callback function.
Also, for future use, the data to be supplied to the match function is separated from the description pointer in the search context. This makes it clear which is being supplied.
---
include/linux/key-type.h | 5 ++ security/keys/internal.h | 40 ++++++++----- security/keys/keyring.c | 70 +++++++++++------------ security/keys/proc.c | 17 ++++-- security/keys/process_keys.c | 117 +++++++++++++++++++------------------- security/keys/request_key.c | 56 +++++++++--------- security/keys/request_key_auth.c | 14 +++-- security/keys/user_defined.c | 18 +++--- 8 files changed, 179 insertions(+), 158 deletions(-)
include/linux/key-type.h +5 -0
@@ -63,6 +63,11 @@ struct key_type {*/ size_t def_datalen; +/* Default key search algorithm. */ +unsigned def_lookup_type; +#define KEYRING_SEARCH_LOOKUP_DIRECT 0x0000/* Direct lookup by description. */ +#define KEYRING_SEARCH_LOOKUP_ITERATE 0x0001/* Iterative search. */ +/* vet a description */ int (*vet_description)(const char *description);
security/keys/internal.h +24 -16
@@ -107,23 +107,31 @@ extern struct key *keyring_search_instkey(struct key *keyring,typedef int (*key_match_func_t )(const struct key *,const void *); +struct keyring_search_context { +struct keyring_index_key index_key; +const struct cred *cred; +key_match_func_t match; +const void *match_data; +unsigned flags; +#define KEYRING_SEARCH_LOOKUP_TYPE 0x0001/* [as type->def_lookup_type] */ +#define KEYRING_SEARCH_NO_STATE_CHECK 0x0002/* Skip state checks */ +#define KEYRING_SEARCH_DO_STATE_CHECK 0x0004/* Override NO_STATE_CHECK */ +#define KEYRING_SEARCH_NO_UPDATE_TIME 0x0008/* Don't update times */ +#define KEYRING_SEARCH_NO_CHECK_PERM 0x0010/* Don't check permissions */ +#define KEYRING_SEARCH_DETECT_TOO_DEEP 0x0020/* Give an error on excessive depth */ + +/* Internal stuff */ +int skipped_ret; +bool possessed; +key_ref_t result; +struct timespec now; +}; +extern key_ref_t keyring_search_aux (key_ref_t keyring_ref, -const struct cred *cred, -struct key_type *type, -const void *description, -key_match_func_t match, -bool no_state_check); - -extern key_ref_t search_my_process_keyrings (struct key_type *type, -const void *description, -key_match_func_t match, -bool no_state_check, -const struct cred *cred); -extern key_ref_t search_process_keyrings (struct key_type *type, -const void *description, -key_match_func_t match, -bool no_state_check, -const struct cred *cred); +struct keyring_search_context *ctx); + +extern key_ref_t search_my_process_keyrings (struct keyring_search_context *ctx); +extern key_ref_t search_process_keyrings (struct keyring_search_context *ctx);extern struct key *find_keyring_by_name (const char *name,bool skip_perm_check);
security/keys/keyring.c +35 -35
167 lines not on this page · raw message
security/keys/proc.c +12 -5
@@ -182,7 +182,6 @@ static void proc_keys_stop(struct seq_file *p, void *v)static int proc_keys_show (struct seq_file *m,void *v) { -const struct cred *cred =current_cred ();struct rb_node *_p = v; struct key *key = rb_entry(_p, struct key, serial_node);struct timespec now;
@@ -191,15 +190,23 @@ static int proc_keys_show(struct seq_file *m, void *v)char xbuf[12 ];int rc; +struct keyring_search_context ctx = { + .index_key.type = key->type, + .index_key.description = key->description, + .cred =current_cred (), + .match = lookup_user_key_possessed, + .match_data = key, + .flags = (KEYRING_SEARCH_NO_STATE_CHECK | +KEYRING_SEARCH_LOOKUP_DIRECT ), + }; + key_ref =make_key_ref (key,0 );/* determine if the key is possessed by this process (a test we can * skip if the key does not indicate the possessor can view it */ if (key->perm &KEY_POS_VIEW ) { - skey_ref =search_my_process_keyrings (key->type, key, - lookup_user_key_possessed, - true, cred); + skey_ref =search_my_process_keyrings (&ctx);if (!IS_ERR (skey_ref)) {key_ref_put (skey_ref); key_ref =make_key_ref (key,1 );
@@ -211,7 +218,7 @@ static int proc_keys_show(struct seq_file *m, void *v)* - the caller holds a spinlock, and thus the RCU read lock, making our * access to __current_cred() safe */ - rc =key_task_permission (key_ref, cred,KEY_VIEW ); + rc =key_task_permission (key_ref, ctx.cred,KEY_VIEW );if (rc <0 )return 0 ;
security/keys/process_keys.c +58 -59
295 lines not on this page · raw message
security/keys/request_key.c +27 -29
137 lines not on this page · raw message
security/keys/request_key_auth.c +8 -6
@@ -239,15 +239,17 @@ static int key_get_instantiation_authkey_match(const struct key *key,*/ struct key *key_get_instantiation_authkey (key_serial_t target_id) { -const struct cred *cred =current_cred (); +struct keyring_search_context ctx = { + .index_key.type = &key_type_request_key_auth, + .cred =current_cred (), + .match = key_get_instantiation_authkey_match, + .match_data = (void *)(unsigned long )target_id, + .flags =KEYRING_SEARCH_LOOKUP_DIRECT , + };struct key *authkey;key_ref_t authkey_ref; - authkey_ref =search_process_keyrings ( - &key_type_request_key_auth, - (void *) (unsigned long ) target_id, - key_get_instantiation_authkey_match, - false, cred); + authkey_ref =search_process_keyrings (&ctx);if (IS_ERR (authkey_ref)) { authkey =ERR_CAST (authkey_ref);
security/keys/user_defined.c +10 -8
@@ -25,14 +25,15 @@ static int logon_vet_description(const char *desc);* arbitrary blob of data as the payload */ struct key_type key_type_user = { - .name ="user" , - .instantiate = user_instantiate, - .update = user_update, - .match = user_match, - .revoke = user_revoke, - .destroy = user_destroy, - .describe = user_describe, - .read = user_read, + .name ="user" , + .def_lookup_type =KEYRING_SEARCH_LOOKUP_DIRECT , + .instantiate = user_instantiate, + .update = user_update, + .match = user_match, + .revoke = user_revoke, + .destroy = user_destroy, + .describe = user_describe, + .read = user_read, };EXPORT_SYMBOL_GPL (key_type_user);
@@ -45,6 +46,7 @@ EXPORT_SYMBOL_GPL(key_type_user);*/ struct key_type key_type_logon = { .name ="logon" , + .def_lookup_type =KEYRING_SEARCH_LOOKUP_DIRECT , .instantiate = user_instantiate, .update = user_update, .match = user_match,
David Howells
maintainerPATCH 7/10 KEYS: Search for auth-key by name rather than targt key ID
In reply to David HowellsSearch for auth-key by name rather than by target key ID as, in a future patch, we'll by searching directly by index key in preference to iteration over all keys.
---
security/keys/request_key_auth.c | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-)
security/keys/request_key_auth.c +7 -14
@@ -18,6 +18,7 @@#include <linux/slab.h> #include <asm/uaccess.h> #include "internal.h" +#include <keys/user-type.h> static int request_key_auth_instantiate (struct key *,struct key_preparsed_payload *);
@@ -222,33 +223,25 @@ error_alloc: }/* -* See if an authorisation key is associated with a particular key. -*/ -static int key_get_instantiation_authkey_match (const struct key *key, -const void *_id) -{ -struct request_key_auth *rka = key->payload.data; -key_serial_t id = (key_serial_t )(unsigned long ) _id; - -return rka->target_key->serial == id; -} - -/* * Search the current process's keyrings for the authorisation key for * instantiation of a key. */ struct key *key_get_instantiation_authkey (key_serial_t target_id) { +char description[16 ];struct keyring_search_context ctx = { .index_key.type = &key_type_request_key_auth, + .index_key.description = description, .cred =current_cred (), - .match = key_get_instantiation_authkey_match, - .match_data = (void *)(unsigned long )target_id, + .match = user_match, + .match_data = description, .flags =KEYRING_SEARCH_LOOKUP_DIRECT , };struct key *authkey;key_ref_t authkey_ref; +sprintf (description,"%x" , target_id); + authkey_ref =search_process_keyrings (&ctx);if (IS_ERR (authkey_ref)) {
David Howells
maintainerPATCH 8/10 KEYS: Define a __key_get() wrapper to use rather than atomic_inc()
In reply to David HowellsDefine a __key_get() wrapper to use rather than atomic_inc() on the key usage count as this makes it easier to hook in refcount error debugging.
---
Documentation/security/keys.txt | 13 ++++++++----- include/linux/key.h | 10 +++++++--- security/keys/key.c | 2 +- security/keys/keyring.c | 6 +++--- security/keys/process_keys.c | 16 ++++++++-------- 5 files changed, 27 insertions(+), 20 deletions(-)
Documentation/security/keys.txt +8 -5
@@ -960,14 +960,17 @@ payload contents" for more information. the argument will not be parsed. -(*) Extra references can be made to a key by calling the following function: +(*) Extra references can be made to a key by calling one of the following + functions: + struct key *__key_get(struct key *key); struct key *key_get(struct key *key); - These need to be disposed of by calling key_put() when they've been - finished with. The key pointer passed in will be returned. If the pointer - is NULL or CONFIG_KEYS is not set then the key will not be dereferenced and - no increment will take place. + Keys so references will need to be disposed of by calling key_put() when + they've been finished with. The key pointer passed in will be returned. + + In the case of key_get(), if the pointer is NULL or CONFIG_KEYS is not set + then the key will not be dereferenced and no increment will take place. (*) A key's serial number can be obtained by calling:
include/linux/key.h +7 -3
@@ -219,13 +219,17 @@ extern void key_revoke(struct key *key);extern void key_invalidate (struct key *key);extern void key_put (struct key *key); -static inline struct key *key_get (struct key *key) +static inline struct key *__key_get (struct key *key) { -if (key) -atomic_inc (&key->usage); +atomic_inc (&key->usage);return key; } +static inline struct key *key_get (struct key *key) +{ +return key ?__key_get (key) : key; +} +static inline void key_ref_put (key_ref_t key_ref) {key_put (key_ref_to_ptr (key_ref));
security/keys/key.c +1 -1
@@ -644,7 +644,7 @@ found:/* this races with key_put(), but that doesn't matter since key_put() * doesn't actually change the key */ -atomic_inc (&key->usage); +__key_get (key); error:spin_unlock (&key_serial_lock);
security/keys/keyring.c +3 -3
@@ -479,7 +479,7 @@ not_this_keyring:/* we found a viable match */ found: -atomic_inc (&key->usage); +__key_get (key); key->last_used_at = ctx->now.tv_sec; keyring->last_used_at = ctx->now.tv_sec;while (sp >0 )
@@ -573,7 +573,7 @@ key_ref_t __keyring_search_one(key_ref_t keyring_ref,return ERR_PTR (-ENOKEY ); found: -atomic_inc (&key->usage); +__key_get (key); keyring->last_used_at = key->last_used_at =current_kernel_time ().tv_sec;rcu_read_unlock ();
@@ -909,7 +909,7 @@ void __key_link(struct key *keyring, struct key *key, klist =rcu_dereference_locked_keyring (keyring); -atomic_inc (&key->usage); +__key_get (key); keyring->last_used_at = key->last_used_at =current_kernel_time ().tv_sec;
security/keys/process_keys.c +8 -8
@@ -235,7 +235,7 @@ int install_session_keyring_to_cred(struct cred *cred, struct key *keyring)if (IS_ERR (keyring))return PTR_ERR (keyring); }else { -atomic_inc (&keyring->usage); +__key_get (keyring); }/* install the keyring */
@@ -544,7 +544,7 @@ try_again: } key = ctx.cred->thread_keyring; -atomic_inc (&key->usage); +__key_get (key); key_ref =make_key_ref (key,1 );break ;
@@ -562,7 +562,7 @@ try_again: } key = ctx.cred->process_keyring; -atomic_inc (&key->usage); +__key_get (key); key_ref =make_key_ref (key,1 );break ;
@@ -593,7 +593,7 @@ try_again:rcu_read_lock (); key =rcu_dereference (ctx.cred->session_keyring); -atomic_inc (&key->usage); +__key_get (key);rcu_read_unlock (); key_ref =make_key_ref (key,1 );break ;
@@ -606,7 +606,7 @@ try_again: } key = ctx.cred->user->uid_keyring; -atomic_inc (&key->usage); +__key_get (key); key_ref =make_key_ref (key,1 );break ;
@@ -618,7 +618,7 @@ try_again: } key = ctx.cred->user->session_keyring; -atomic_inc (&key->usage); +__key_get (key); key_ref =make_key_ref (key,1 );break ;
@@ -632,7 +632,7 @@ try_again:if (!key) goto error; -atomic_inc (&key->usage); +__key_get (key); key_ref =make_key_ref (key,1 );break ;
@@ -648,7 +648,7 @@ try_again: }else { rka = ctx.cred->request_key_auth->payload.data; key = rka->dest_keyring; -atomic_inc (&key->usage); +__key_get (key); }up_read (&ctx.cred->request_key_auth->sem);if (!key)
David Howells
maintainerPATCH 9/10 KEYS: Drop the permissions argument from __keyring_search_one()
In reply to David HowellsDrop the permissions argument from __keyring_search_one() as the only caller passes 0 here - which causes all checks to be skipped.
---
security/keys/internal.h | 3 +-- security/keys/key.c | 2 +- security/keys/keyring.c | 9 +++------ 3 files changed, 5 insertions(+), 9 deletions(-)
security/keys/internal.h +1 -2
@@ -99,8 +99,7 @@ extern void __key_link_end(struct key *keyring, unsigned long prealloc);extern key_ref_t __keyring_search_one (key_ref_t keyring_ref, -const struct keyring_index_key *index_key, -key_perm_t perm); +const struct keyring_index_key *index_key);extern struct key *keyring_search_instkey (struct key *keyring,key_serial_t target_id);
security/keys/key.c +1 -1
@@ -847,7 +847,7 @@ key_ref_t key_create_or_update(key_ref_t keyring_ref,* update that instead if possible */ if (index_key.type->update) { - key_ref =__keyring_search_one (keyring_ref, &index_key,0 ); + key_ref =__keyring_search_one (keyring_ref, &index_key);if (!IS_ERR (key_ref)) goto found_matching_key; }
security/keys/keyring.c +3 -6
@@ -531,15 +531,14 @@ EXPORT_SYMBOL(keyring_search);* RCU is used to make it unnecessary to lock the keyring key list here. * * Returns a pointer to the found key with usage count incremented if -* successful and returns -ENOKEY if not found. Revoked keys and keys not -* providing the requested permission are skipped over. +* successful and returns -ENOKEY if not found. Revoked and invalidated keys +* are skipped over. * * If successful, the possession indicator is propagated from the keyring ref * to the returned key reference. */ key_ref_t __keyring_search_one (key_ref_t keyring_ref, -const struct keyring_index_key *index_key, -key_perm_t perm) +const struct keyring_index_key *index_key) {struct keyring_list *klist;struct key *keyring, *key;
@@ -560,8 +559,6 @@ key_ref_t __keyring_search_one(key_ref_t keyring_ref,if (key->type == index_key->type && (!key->type->match || key->type->match (key, index_key->description)) && -key_permission (make_key_ref (key, possessed), - perm) ==0 && !(key->flags & ((1 <<KEY_FLAG_INVALIDATED ) | (1 <<KEY_FLAG_REVOKED ))) )
David Howells
maintainerPATCH 10/10 KEYS: Expand the capacity of a keyring
In reply to David HowellsExpand the capacity of a keyring to be able to hold a lot more keys by using the previously added associative array implementation. Currently the maximum capacity is:
(PAGE_SIZE - sizeof(header)) / sizeof(struct key *)
which, on a 64-bit system, is a little more 500. However, since this is being used for the NFS uid mapper, we need more than that. The new implementation gives us effectively unlimited capacity.
With some alterations, the keyutils testsuite runs successfully to completion after this patch is applied. The alterations are because (a) keyrings that are simply added to no longer appear ordered and (b) some of the errors have changed a bit.
---
include/keys/keyring-type.h | 17 - include/linux/key.h | 13 security/keys/Kconfig | 1 security/keys/gc.c | 30 - security/keys/internal.h | 17 - security/keys/key.c | 35 + security/keys/keyring.c | 1402 ++++++++++++++++++++++--------------------- security/keys/request_key.c | 12 8 files changed, 767 insertions(+), 760 deletions(-)
include/keys/keyring-type.h +2 -15
@@ -1,6 +1,6 @@/* Keyring key type * -* Copyright (C) 2008 Red Hat, Inc. All Rights Reserved. +* Copyright (C) 2008, 2013 Red Hat, Inc. All Rights Reserved. * Written by David Howells ([email protected]) * * This program is free software; you can redistribute it and/or
@@ -13,19 +13,6 @@#define _KEYS_KEYRING_TYPE_H#include <linux/key.h> -#include <linux/rcupdate.h> - -/* -* the keyring payload contains a list of the keys to which the keyring is -* subscribed -*/ -struct keyring_list { -struct rcu_head rcu;/* RCU deletion hook */ -unsigned short maxkeys;/* max keys this list can hold */ -unsigned short nkeys;/* number of keys currently held */ -unsigned short delkey;/* key to be unlinked by RCU */ -struct key __rcu *keys[0 ]; -}; - +#include <linux/assoc_array.h> #endif /* _KEYS_KEYRING_TYPE_H */
include/linux/key.h +8 -5
@@ -22,6 +22,7 @@#include <linux/sysctl.h> #include <linux/rwsem.h> #include <linux/atomic.h> +#include <linux/assoc_array.h> #ifdef __KERNEL__#include <linux/uidgid.h>
@@ -196,11 +197,13 @@ struct key {* whatever */ union { -unsigned long value; -void __rcu *rcudata; -void *data; -struct keyring_list __rcu *subscriptions; - } payload; +union { +unsigned long value; +void __rcu *rcudata; +void *data; + } payload; +struct assoc_array keys; + }; }; extern struct key *key_alloc(struct key_type *type,
security/keys/Kconfig +1 -0
@@ -4,6 +4,7 @@ config KEYS bool "Enable access key retention support" + select ASSOCIATIVE_ARRAY help This option provides support for retaining authentication tokens and access keys in the kernel.
security/keys/gc.c +13 -17
@@ -130,6 +130,13 @@ void key_gc_keytype(struct key_type *ktype)kleave ("" ); } +static int key_gc_keyring_func (const void *object,bool type,void *iterator_data) +{ +const struct key *key = object; +time_t *limit = iterator_data; +return key_is_dead (key, *limit); +} +/* * Garbage collect pointers from a keyring. *
@@ -138,8 +145,7 @@ void key_gc_keytype(struct key_type *ktype)*/ static void key_gc_keyring (struct key *keyring,time_t limit) { -struct keyring_list *klist; -int loop; +int result;kenter ("%x" ,key_serial (keyring));
@@ -149,27 +155,17 @@ static void key_gc_keyring(struct key *keyring, time_t limit)/* scan the keyring looking for dead keys */ rcu_read_lock (); - klist =rcu_dereference (keyring->payload.subscriptions); -if (!klist) - goto unlock_dont_gc; - - loop = klist->nkeys; -smp_rmb (); -for (loop--; loop >=0 ; loop--) { -struct key *key =rcu_dereference (klist->keys[loop]); -if (key_is_dead (key, limit)) - goto do_gc; - } - -unlock_dont_gc: + result =assoc_array_iterate (&keyring->keys, + key_gc_keyring_func, &limit);rcu_read_unlock (); +if (result ==0 ) + goto do_gc; + dont_gc:kleave (" [no gc]" );return ; do_gc: -rcu_read_unlock (); -keyring_gc (keyring, limit);kleave (" [gc]" ); }
security/keys/internal.h +11 -6
@@ -90,20 +90,23 @@ extern void key_type_put(struct key_type *ktype);extern int __key_link_begin (struct key *keyring,const struct keyring_index_key *index_key, -unsigned long *_prealloc); +struct assoc_array_edit **_edit);extern int __key_link_check_live_key (struct key *keyring,struct key *key); -extern void __key_link (struct key *keyring,struct key *key, -unsigned long *_prealloc); +extern void __key_link (struct key *key,struct assoc_array_edit **_edit);extern void __key_link_end (struct key *keyring,const struct keyring_index_key *index_key, -unsigned long prealloc); +struct assoc_array_edit *edit); -extern key_ref_t __keyring_search_one (key_ref_t keyring_ref, -const struct keyring_index_key *index_key); +extern key_ref_t find_key_to_update (key_ref_t keyring_ref, +const struct keyring_index_key *index_key);extern struct key *keyring_search_instkey (struct key *keyring,key_serial_t target_id); +extern int iterate_over_keyring (const struct key *keyring, +int (*func)(const struct key *key,void *data), +void *data); +typedef int (*key_match_func_t )(const struct key *,const void *);struct keyring_search_context {
@@ -119,6 +122,8 @@ struct keyring_search_context {#define KEYRING_SEARCH_NO_CHECK_PERM 0x0010/* Don't check permissions */ #define KEYRING_SEARCH_DETECT_TOO_DEEP 0x0020/* Give an error on excessive depth */ +int (*iterator)(const void *object,bool type,void *iterator_data); +/* Internal stuff */ int skipped_ret;bool possessed;
security/keys/key.c +17 -18
@@ -409,7 +409,7 @@ static int __key_instantiate_and_link(struct key *key,struct key_preparsed_payload *prep, struct key *keyring, struct key *authkey, - unsigned long *_prealloc) + struct index_tree_edit **_edit) { int ret, awaken;
@@ -436,7 +436,7 @@ static int __key_instantiate_and_link(struct key *key,/* and link it into the destination keyring */ if (keyring) -__key_link (keyring, key, _prealloc); +__key_link (key, _edit);/* disable the authorisation key */ if (authkey)
@@ -476,7 +476,7 @@ int key_instantiate_and_link(struct key *key, struct key *authkey) {struct key_preparsed_payload prep; -unsigned long prealloc; +struct index_tree_edit *edit;int ret;memset (&prep,0 ,sizeof (prep));
@@ -490,16 +490,15 @@ int key_instantiate_and_link(struct key *key, }if (keyring) { - ret =__key_link_begin (keyring, &key->index_key, &prealloc); + ret =__key_link_begin (keyring, &key->index_key, &edit);if (ret <0 ) goto error_free_preparse; } - ret =__key_instantiate_and_link (key, &prep, keyring, authkey, - &prealloc); + ret =__key_instantiate_and_link (key, &prep, keyring, authkey, &edit);if (keyring) -__key_link_end (keyring, &key->index_key, prealloc); +__key_link_end (keyring, &key->index_key, edit); error_free_preparse:if (key->type->preparse)
@@ -537,7 +536,7 @@ int key_reject_and_link(struct key *key,struct key *keyring, struct key *authkey) { - unsigned long prealloc; + struct index_tree_edit *edit;struct timespec now;int ret, awaken, link_ret =0 ;
@@ -548,7 +547,7 @@ int key_reject_and_link(struct key *key, ret = -EBUSY ;if (keyring) - link_ret =__key_link_begin (keyring, &key->index_key, &prealloc); + link_ret =__key_link_begin (keyring, &key->index_key, &edit);mutex_lock (&key_construction_mutex);
@@ -570,7 +569,7 @@ int key_reject_and_link(struct key *key,/* and link it into the destination keyring */ if (keyring && link_ret ==0 ) -__key_link (keyring, key, &prealloc); +__key_link (key, &edit);/* disable the authorisation key */ if (authkey)
@@ -580,7 +579,7 @@ int key_reject_and_link(struct key *key,mutex_unlock (&key_construction_mutex);if (keyring) -__key_link_end (keyring, &key->index_key, prealloc); +__key_link_end (keyring, &key->index_key, edit);/* wake up anyone waiting for a key to be constructed */ if (awaken)
@@ -783,8 +782,8 @@ key_ref_t key_create_or_update(key_ref_t keyring_ref, .description = description, };struct key_preparsed_payload prep; +struct index_tree_edit *edit;const struct cred *cred =current_cred (); -unsigned long prealloc;struct key *keyring, *key =NULL ;key_ref_t key_ref;int ret;
@@ -828,7 +827,7 @@ key_ref_t key_create_or_update(key_ref_t keyring_ref, } index_key.desc_len =strlen (index_key.description); - ret =__key_link_begin (keyring, &index_key, &prealloc); + ret =__key_link_begin (keyring, &index_key, &edit);if (ret <0 ) { key_ref =ERR_PTR (ret); goto error_free_prep;
@@ -847,8 +846,8 @@ key_ref_t key_create_or_update(key_ref_t keyring_ref,* update that instead if possible */ if (index_key.type->update) { - key_ref =__keyring_search_one (keyring_ref, &index_key); -if (!IS_ERR (key_ref)) + key_ref =find_key_to_update (keyring_ref, &index_key); +if (key_ref) goto found_matching_key; }
@@ -874,7 +873,7 @@ key_ref_t key_create_or_update(key_ref_t keyring_ref, }/* instantiate it and link it into the target keyring */ - ret =__key_instantiate_and_link (key, &prep, keyring,NULL , &prealloc); + ret =__key_instantiate_and_link (key, &prep, keyring,NULL , &edit);if (ret <0 ) {key_put (key); key_ref =ERR_PTR (ret);
@@ -884,7 +883,7 @@ key_ref_t key_create_or_update(key_ref_t keyring_ref, key_ref =make_key_ref (key,is_key_possessed (keyring_ref)); error_link_end: -__key_link_end (keyring, &index_key, prealloc); +__key_link_end (keyring, &index_key, edit); error_free_prep:if (index_key.type->preparse) index_key.type->free_preparse (&prep);
@@ -897,7 +896,7 @@ error:/* we found a matching key, so we're going to try to update it * - we can drop the locks first as we have the key pinned */ -__key_link_end (keyring, &index_key, prealloc); +__key_link_end (keyring, &index_key, edit); key_ref =__key_update (key_ref, &prep); goto error_free_prep;
security/keys/keyring.c +709 -693
1,706 lines not on this page · raw message
security/keys/request_key.c +6 -6
@@ -351,7 +351,7 @@ static int construct_alloc_key(struct keyring_search_context *ctx,struct key_user *user, struct key **_key) { - unsigned long prealloc; + struct index_tree_edit *edit;struct key *key;key_perm_t perm;key_ref_t key_ref;
@@ -380,7 +380,7 @@ static int construct_alloc_key(struct keyring_search_context *ctx,set_bit (KEY_FLAG_USER_CONSTRUCT , &key->flags);if (dest_keyring) { - ret =__key_link_begin (dest_keyring, &ctx->index_key, &prealloc); + ret =__key_link_begin (dest_keyring, &ctx->index_key, &edit);if (ret <0 ) goto link_prealloc_failed; }
@@ -395,11 +395,11 @@ static int construct_alloc_key(struct keyring_search_context *ctx, goto key_already_present;if (dest_keyring) -__key_link (dest_keyring, key, &prealloc); +__key_link (key, &edit);mutex_unlock (&key_construction_mutex);if (dest_keyring) -__key_link_end (dest_keyring, &ctx->index_key, prealloc); +__key_link_end (dest_keyring, &ctx->index_key, edit);mutex_unlock (&user->cons_lock); *_key = key;kleave (" = 0 [%d]" ,key_serial (key));
@@ -414,8 +414,8 @@ key_already_present:if (dest_keyring) { ret =__key_link_check_live_key (dest_keyring, key);if (ret ==0 ) -__key_link (dest_keyring, key, &prealloc); -__key_link_end (dest_keyring, &ctx->index_key, prealloc); +__key_link (key, &edit); +__key_link_end (dest_keyring, &ctx->index_key, edit);if (ret <0 ) goto link_check_failed; }