libsubid: Add routine to free allocated memory

This commit is contained in:
Daniel Bershatsky 2024-06-11 01:12:45 +03:00 committed by Alejandro Colomar
parent 18f113cc46
commit 0217516349
7 changed files with 48 additions and 1 deletions

View File

@ -131,6 +131,11 @@ void nss_init(const char *nsswitch_path) {
fprintf(shadow_logfd, "%s did not provide @find_subid_owners@\n", libname);
goto close_lib;
}
subid_nss->free= dlsym(h, "shadow_subid_free");
if (!subid_nss->free) {
// Fallback to default `free()` for backward compatibility.
subid_nss->free = free;
}
subid_nss->handle = h;
goto done;

View File

@ -284,6 +284,19 @@ struct subid_nss_ops {
*/
enum subid_status (*find_subid_owners)(unsigned long id, enum subid_type id_type, uid_t **uids, int *count);
/*
* nss_free: free a memory block allocated by a subid plugin.
*
* @ptr - a pointer to a memory block to deallocate
*
* Some routines of subid_nss_ops allocate memory which should be freed by
* caller after use. In order to deallocate that memory block, one should
* use this routine to release that memory. By default, this function
* pointer is set to free() for backward compatibility. However, it is
* strongly recommended to define this routine explicitly.
*/
void (*free)(void *ptr);
/* The dlsym handle to close */
void *handle;
};

View File

@ -1117,6 +1117,15 @@ bool release_subid_range(struct subordinate_range *range, enum subid_type id_typ
return ret;
}
void free_subid_pointer(void *ptr) {
struct subid_nss_ops *h = get_subid_nss_handle();
if (h) {
h->free(ptr);
} else {
free(ptr);
}
}
#else /* !ENABLE_SUBIDS */
extern int ISO_C_forbids_an_empty_translation_unit;
#endif /* !ENABLE_SUBIDS */

View File

@ -43,6 +43,9 @@ extern int sub_gid_unlock (void);
extern int sub_gid_add (const char *owner, gid_t start, unsigned long count);
extern int sub_gid_remove (const char *owner, gid_t start, unsigned long count);
extern uid_t sub_gid_find_free_range(gid_t min, gid_t max, unsigned long count);
extern void free_subid_pointer(void *ptr);
#endif /* ENABLE_SUBIDS */
#endif

View File

@ -42,6 +42,10 @@ bool subid_init(const char *progname, FILE * logfd)
return true;
}
void subid_free(void *ptr) {
free_subid_pointer(ptr);
}
static
int get_subid_ranges(const char *owner, enum subid_type id_type, struct subid_range **ranges)
{

View File

@ -55,6 +55,19 @@ extern "C" {
*/
bool subid_init(const char *progname, FILE *logfd);
/*
* subid_free: free memory allocated in any subid_* function
*
* @ptr: Pointer to a memory block to release.
*
* Some functions like @subid_get_uid_ranges allocate memory internally. As
* soon as a result is no longer needed, it should be freed with this routine.
* Initially, default function `free()` was used. Thus for backward
* compatibility this function falls back to `free()` if a plugin does not
* explicitly specify routine to free allocated memory.
*/
void subid_free(void *ptr);
/*
* subid_get_uid_ranges: return a list of UID ranges for a user
*

View File

@ -44,6 +44,6 @@ int main(int argc, char *argv[])
printf("%d: %s %lu %lu\n", i, owner,
ranges[i].start, ranges[i].count);
}
free(ranges);
subid_free(ranges);
return 0;
}