cred-encoding: Avoid potential use after free when caching encodings
The pattern currently is to call get_cache(), generate the encoding if that failed and then store it with cache(). The latter adopts the passed encoding and frees any stored encoding. However, the latter means that if two threads concurrently fail to get a cached encoding and then both generate and store one, one of the threads might use an encoding that was freed by the other thread. Since encodings are not expected to change, we can avoid this issue by not replacing an existing cache entry and instead return that (while freeing the passed value instead of the cached one). Closes strongswan/strongswan#1231
This commit is contained in:
@@ -202,14 +202,16 @@ struct cred_encoding_t {
|
||||
* Cache a credential encoding created externally.
|
||||
*
|
||||
* After calling cache(), the passed encoding is owned by the cred encoding
|
||||
* facility.
|
||||
* facility. Note that if there already is a cached encoding for the same
|
||||
* key, the method will return that and free the passed value.
|
||||
*
|
||||
* @param type format of the credential encoding
|
||||
* @param cache key to use for caching, as given to encode()
|
||||
* @param encoding encoding to cache, gets owned by this
|
||||
* @param encoding encoding to cache, gets adopted, might get replaced
|
||||
* with cached value
|
||||
*/
|
||||
void (*cache)(cred_encoding_t *this, cred_encoding_type_t type, void *cache,
|
||||
chunk_t encoding);
|
||||
chunk_t *encoding);
|
||||
|
||||
/**
|
||||
* Register a credential encoder function.
|
||||
|
||||
Reference in New Issue
Block a user