Skip to content

Commit

Permalink
SSS_CLIENT: check if mem-cache fd was hijacked
Browse files Browse the repository at this point in the history
  • Loading branch information
alexey-tikhonov committed Dec 15, 2023
1 parent 5da9617 commit 75505c1
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/sss_client/nss_mc.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ struct sss_cli_mc_ctx {
pthread_mutex_t *mutex;
#endif
int fd;
ino_t fd_inode;

uint32_t seed; /* seed from the tables header */

Expand All @@ -69,9 +70,9 @@ struct sss_cli_mc_ctx {
};

#if HAVE_PTHREAD
#define SSS_CLI_MC_CTX_INITIALIZER(mtx) {UNINITIALIZED, (mtx), -1, 0, NULL, 0, NULL, 0, NULL, 0, 0}
#define SSS_CLI_MC_CTX_INITIALIZER(mtx) {UNINITIALIZED, (mtx), -1, 0, 0, NULL, 0, NULL, 0, NULL, 0, 0}
#else
#define SSS_CLI_MC_CTX_INITIALIZER {UNINITIALIZED, -1, 0, NULL, 0, NULL, 0, NULL, 0, 0}
#define SSS_CLI_MC_CTX_INITIALIZER {UNINITIALIZED, -1, 0, 0, NULL, 0, NULL, 0, NULL, 0, 0}
#endif

errno_t sss_nss_mc_get_ctx(const char *name, struct sss_cli_mc_ctx *ctx);
Expand Down
8 changes: 8 additions & 0 deletions src/sss_client/nss_mc_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,12 @@ static errno_t sss_nss_mc_validate(struct sss_cli_mc_ctx *ctx)
return EINVAL;
}

/* FD was hijacked */
if (fdstat.st_ino != ctx->fd_inode) {
ctx->fd = -1; /* don't ruin app even if it's misbehaving */
return EINVAL;
}

/* Invalid size. */
if (fdstat.st_size != ctx->mmap_size) {
return EINVAL;
Expand Down Expand Up @@ -161,6 +167,7 @@ static void sss_nss_mc_destroy_ctx(struct sss_cli_mc_ctx *ctx)
close(ctx->fd);
}
ctx->fd = -1;
ctx->fd_inode = 0;

ctx->seed = 0;
ctx->data_table = NULL;
Expand Down Expand Up @@ -202,6 +209,7 @@ static errno_t sss_nss_mc_init_ctx(const char *name,
ret = EIO;
goto done;
}
ctx->fd_inode = fdstat.st_ino;

if (fdstat.st_size < MC_HEADER_SIZE) {
ret = ENOMEM;
Expand Down

0 comments on commit 75505c1

Please sign in to comment.