Skip to content

Commit

Permalink
MC: a couple of additions to 'recover from invalid memory cache size'…
Browse files Browse the repository at this point in the history
… patch

Additions to 641e5f7 :

 - handle all invalidations consistently
 - supply a valid pointer to `sss_mmap_cache_validate_or_reinit()`,
   not a pointer to a local var

Reviewed-by: Pavel Březina <[email protected]>
Reviewed-by: Sumit Bose <[email protected]>
(cherry picked from commit 88d8afb)
  • Loading branch information
alexey-tikhonov committed Oct 2, 2023
1 parent 0047969 commit 0705145
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 36 deletions.
10 changes: 5 additions & 5 deletions src/responder/nss/nss_get_object.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ memcache_delete_entry_by_name(struct sss_nss_ctx *nss_ctx,

switch (type) {
case SSS_MC_PASSWD:
ret = sss_mmap_cache_pw_invalidate(nss_ctx->pwd_mc_ctx, name);
ret = sss_mmap_cache_pw_invalidate(&nss_ctx->pwd_mc_ctx, name);
break;
case SSS_MC_GROUP:
ret = sss_mmap_cache_gr_invalidate(nss_ctx->grp_mc_ctx, name);
ret = sss_mmap_cache_gr_invalidate(&nss_ctx->grp_mc_ctx, name);
break;
case SSS_MC_INITGROUPS:
ret = sss_mmap_cache_initgr_invalidate(nss_ctx->initgr_mc_ctx, name);
ret = sss_mmap_cache_initgr_invalidate(&nss_ctx->initgr_mc_ctx, name);
break;
default:
return EINVAL;
Expand All @@ -66,10 +66,10 @@ memcache_delete_entry_by_id(struct sss_nss_ctx *nss_ctx,

switch (type) {
case SSS_MC_PASSWD:
ret = sss_mmap_cache_pw_invalidate_uid(nss_ctx->pwd_mc_ctx, (uid_t)id);
ret = sss_mmap_cache_pw_invalidate_uid(&nss_ctx->pwd_mc_ctx, (uid_t)id);
break;
case SSS_MC_GROUP:
ret = sss_mmap_cache_gr_invalidate_gid(nss_ctx->grp_mc_ctx, (gid_t)id);
ret = sss_mmap_cache_gr_invalidate_gid(&nss_ctx->grp_mc_ctx, (gid_t)id);
break;
default:
return EINVAL;
Expand Down
8 changes: 4 additions & 4 deletions src/responder/nss/nss_iface.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ sss_nss_update_initgr_memcache(struct sss_nss_ctx *nctx,

if (ret == ENOENT || res->count == 0) {
/* The user is gone. Invalidate the mc record */
ret = sss_mmap_cache_pw_invalidate(nctx->pwd_mc_ctx, delete_name);
ret = sss_mmap_cache_pw_invalidate(&nctx->pwd_mc_ctx, delete_name);
if (ret != EOK && ret != ENOENT) {
DEBUG(SSSDBG_CRIT_FAILURE,
"Internal failure in memory cache code: %d [%s]\n",
Expand Down Expand Up @@ -125,7 +125,7 @@ sss_nss_update_initgr_memcache(struct sss_nss_ctx *nctx,
for (i = 0; i < gnum; i++) {
id = groups[i];

ret = sss_mmap_cache_gr_invalidate_gid(nctx->grp_mc_ctx, id);
ret = sss_mmap_cache_gr_invalidate_gid(&nctx->grp_mc_ctx, id);
if (ret != EOK && ret != ENOENT) {
DEBUG(SSSDBG_CRIT_FAILURE,
"Internal failure in memory cache code: %d [%s]\n",
Expand All @@ -134,7 +134,7 @@ sss_nss_update_initgr_memcache(struct sss_nss_ctx *nctx,
}

to_sized_string(delete_name, fq_name);
ret = sss_mmap_cache_initgr_invalidate(nctx->initgr_mc_ctx,
ret = sss_mmap_cache_initgr_invalidate(&nctx->initgr_mc_ctx,
delete_name);
if (ret != EOK && ret != ENOENT) {
DEBUG(SSSDBG_CRIT_FAILURE,
Expand Down Expand Up @@ -208,7 +208,7 @@ sss_nss_memorycache_invalidate_group_by_id(TALLOC_CTX *mem_ctx,
DEBUG(SSSDBG_TRACE_LIBS,
"Invalidating group %u from memory cache\n", gid);

sss_mmap_cache_gr_invalidate_gid(nctx->grp_mc_ctx, gid);
sss_mmap_cache_gr_invalidate_gid(&nctx->grp_mc_ctx, gid);

return EOK;
}
Expand Down
64 changes: 42 additions & 22 deletions src/responder/nss/nsssrv_mmap_cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -701,16 +701,22 @@ static inline void sss_mmap_chain_in_rec(struct sss_mc_ctx *mcc,
* generic invalidation
***************************************************************************/

static errno_t sss_mmap_cache_invalidate(struct sss_mc_ctx *mcc,
static errno_t sss_mmap_cache_validate_or_reinit(struct sss_mc_ctx **_mcc);

static errno_t sss_mmap_cache_invalidate(struct sss_mc_ctx **_mcc,
const struct sized_string *key)
{
struct sss_mc_ctx *mcc;
struct sss_mc_rec *rec;
int ret;

if (mcc == NULL) {
/* cache not initialized? */
return EINVAL;
ret = sss_mmap_cache_validate_or_reinit(_mcc);
if (ret != EOK) {
return ret;
}

mcc = *_mcc;

rec = sss_mc_find_record(mcc, key);
if (rec == NULL) {
/* nothing to invalidate */
Expand Down Expand Up @@ -785,7 +791,7 @@ errno_t sss_mmap_cache_pw_store(struct sss_mc_ctx **_mcc,
const struct sized_string *homedir,
const struct sized_string *shell)
{
struct sss_mc_ctx *mcc = *_mcc;
struct sss_mc_ctx *mcc;
struct sss_mc_rec *rec;
struct sss_mc_pwd_data *data;
struct sized_string uidkey;
Expand All @@ -795,11 +801,13 @@ errno_t sss_mmap_cache_pw_store(struct sss_mc_ctx **_mcc,
size_t pos;
int ret;

ret = sss_mmap_cache_validate_or_reinit(&mcc);
ret = sss_mmap_cache_validate_or_reinit(_mcc);
if (ret != EOK) {
return ret;
}

mcc = *_mcc;

ret = snprintf(uidstr, 11, "%ld", (long)uid);
if (ret > 10) {
return EINVAL;
Expand Down Expand Up @@ -851,26 +859,29 @@ errno_t sss_mmap_cache_pw_store(struct sss_mc_ctx **_mcc,
return EOK;
}

errno_t sss_mmap_cache_pw_invalidate(struct sss_mc_ctx *mcc,
errno_t sss_mmap_cache_pw_invalidate(struct sss_mc_ctx **_mcc,
const struct sized_string *name)
{
return sss_mmap_cache_invalidate(mcc, name);
return sss_mmap_cache_invalidate(_mcc, name);
}

errno_t sss_mmap_cache_pw_invalidate_uid(struct sss_mc_ctx *mcc, uid_t uid)
errno_t sss_mmap_cache_pw_invalidate_uid(struct sss_mc_ctx **_mcc, uid_t uid)
{
struct sss_mc_ctx *mcc;
struct sss_mc_rec *rec = NULL;
struct sss_mc_pwd_data *data;
uint32_t hash;
uint32_t slot;
char *uidstr;
errno_t ret;

ret = sss_mmap_cache_validate_or_reinit(&mcc);
ret = sss_mmap_cache_validate_or_reinit(_mcc);
if (ret != EOK) {
return ret;
}

mcc = *_mcc;

uidstr = talloc_asprintf(NULL, "%ld", (long)uid);
if (!uidstr) {
return ENOMEM;
Expand Down Expand Up @@ -927,7 +938,7 @@ int sss_mmap_cache_gr_store(struct sss_mc_ctx **_mcc,
gid_t gid, size_t memnum,
const char *membuf, size_t memsize)
{
struct sss_mc_ctx *mcc = *_mcc;
struct sss_mc_ctx *mcc;
struct sss_mc_rec *rec;
struct sss_mc_grp_data *data;
struct sized_string gidkey;
Expand All @@ -937,11 +948,13 @@ int sss_mmap_cache_gr_store(struct sss_mc_ctx **_mcc,
size_t pos;
int ret;

ret = sss_mmap_cache_validate_or_reinit(&mcc);
ret = sss_mmap_cache_validate_or_reinit(_mcc);
if (ret != EOK) {
return ret;
}

mcc = *_mcc;

ret = snprintf(gidstr, 11, "%ld", (long)gid);
if (ret > 10) {
return EINVAL;
Expand Down Expand Up @@ -989,26 +1002,29 @@ int sss_mmap_cache_gr_store(struct sss_mc_ctx **_mcc,
return EOK;
}

errno_t sss_mmap_cache_gr_invalidate(struct sss_mc_ctx *mcc,
errno_t sss_mmap_cache_gr_invalidate(struct sss_mc_ctx **_mcc,
const struct sized_string *name)
{
return sss_mmap_cache_invalidate(mcc, name);
return sss_mmap_cache_invalidate(_mcc, name);
}

errno_t sss_mmap_cache_gr_invalidate_gid(struct sss_mc_ctx *mcc, gid_t gid)
errno_t sss_mmap_cache_gr_invalidate_gid(struct sss_mc_ctx **_mcc, gid_t gid)
{
struct sss_mc_ctx *mcc;
struct sss_mc_rec *rec = NULL;
struct sss_mc_grp_data *data;
uint32_t hash;
uint32_t slot;
char *gidstr;
errno_t ret;

ret = sss_mmap_cache_validate_or_reinit(&mcc);
ret = sss_mmap_cache_validate_or_reinit(_mcc);
if (ret != EOK) {
return ret;
}

mcc = *_mcc;

gidstr = talloc_asprintf(NULL, "%ld", (long)gid);
if (!gidstr) {
return ENOMEM;
Expand Down Expand Up @@ -1061,19 +1077,21 @@ errno_t sss_mmap_cache_initgr_store(struct sss_mc_ctx **_mcc,
uint32_t num_groups,
const uint8_t *gids_buf)
{
struct sss_mc_ctx *mcc = *_mcc;
struct sss_mc_ctx *mcc;
struct sss_mc_rec *rec;
struct sss_mc_initgr_data *data;
size_t data_len;
size_t rec_len;
size_t pos;
int ret;

ret = sss_mmap_cache_validate_or_reinit(&mcc);
ret = sss_mmap_cache_validate_or_reinit(_mcc);
if (ret != EOK) {
return ret;
}

mcc = *_mcc;

/* array of gids + name + unique_name */
data_len = num_groups * sizeof(uint32_t) + name->len + unique_name->len;
rec_len = sizeof(struct sss_mc_rec) + sizeof(struct sss_mc_initgr_data)
Expand Down Expand Up @@ -1119,10 +1137,10 @@ errno_t sss_mmap_cache_initgr_store(struct sss_mc_ctx **_mcc,
return EOK;
}

errno_t sss_mmap_cache_initgr_invalidate(struct sss_mc_ctx *mcc,
errno_t sss_mmap_cache_initgr_invalidate(struct sss_mc_ctx **_mcc,
const struct sized_string *name)
{
return sss_mmap_cache_invalidate(mcc, name);
return sss_mmap_cache_invalidate(_mcc, name);
}

errno_t sss_mmap_cache_sid_store(struct sss_mc_ctx **_mcc,
Expand All @@ -1131,18 +1149,20 @@ errno_t sss_mmap_cache_sid_store(struct sss_mc_ctx **_mcc,
uint32_t type,
bool explicit_lookup)
{
struct sss_mc_ctx *mcc = *_mcc;
struct sss_mc_ctx *mcc;
struct sss_mc_rec *rec;
struct sss_mc_sid_data *data;
char idkey[16];
size_t rec_len;
int ret;

ret = sss_mmap_cache_validate_or_reinit(&mcc);
ret = sss_mmap_cache_validate_or_reinit(_mcc);
if (ret != EOK) {
return ret;
}

mcc = *_mcc;

ret = snprintf(idkey, sizeof(idkey), "%d-%ld",
(type == SSS_ID_TYPE_GID) ? SSS_ID_TYPE_GID : SSS_ID_TYPE_UID,
(long)id);
Expand Down
10 changes: 5 additions & 5 deletions src/responder/nss/nsssrv_mmap_cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,17 @@ errno_t sss_mmap_cache_sid_store(struct sss_mc_ctx **_mcc,
uint32_t type, /* enum sss_id_type*/
bool explicit_lookup); /* false ~ by_id(), true ~ by_uid/gid() */

errno_t sss_mmap_cache_pw_invalidate(struct sss_mc_ctx *mcc,
errno_t sss_mmap_cache_pw_invalidate(struct sss_mc_ctx **_mcc,
const struct sized_string *name);

errno_t sss_mmap_cache_pw_invalidate_uid(struct sss_mc_ctx *mcc, uid_t uid);
errno_t sss_mmap_cache_pw_invalidate_uid(struct sss_mc_ctx **_mcc, uid_t uid);

errno_t sss_mmap_cache_gr_invalidate(struct sss_mc_ctx *mcc,
errno_t sss_mmap_cache_gr_invalidate(struct sss_mc_ctx **_mcc,
const struct sized_string *name);

errno_t sss_mmap_cache_gr_invalidate_gid(struct sss_mc_ctx *mcc, gid_t gid);
errno_t sss_mmap_cache_gr_invalidate_gid(struct sss_mc_ctx **_mcc, gid_t gid);

errno_t sss_mmap_cache_initgr_invalidate(struct sss_mc_ctx *mcc,
errno_t sss_mmap_cache_initgr_invalidate(struct sss_mc_ctx **_mcc,
const struct sized_string *name);

errno_t sss_mmap_cache_reinit(TALLOC_CTX *mem_ctx,
Expand Down

0 comments on commit 0705145

Please sign in to comment.