Skip to content

Commit d89b22d

Browse files
NeilBrownamschuma-ntap
authored andcommitted
cred: add cred_fscmp() for comparing creds.
NFS needs to compare to credentials, to see if they can be treated the same w.r.t. filesystem access. Sometimes an ordering is needed when credentials are used as a key to an rbtree. NFS currently has its own private credential management from before 'struct cred' existed. To move it over to more consistent use of 'struct cred' we need a comparison function. This patch adds that function. Signed-off-by: NeilBrown <neilb@suse.com> Signed-off-by: Anna Schumaker <Anna.Schumaker@Netapp.com>
1 parent 8e2e5b7 commit d89b22d

2 files changed

Lines changed: 56 additions & 0 deletions

File tree

‎include/linux/cred.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ extern int change_create_files_as(struct cred *, struct inode *);
169169
extern int set_security_override(struct cred *, u32);
170170
extern int set_security_override_from_ctx(struct cred *, const char *);
171171
extern int set_create_files_as(struct cred *, struct inode *);
172+
extern int cred_fscmp(const struct cred *, const struct cred *);
172173
extern void __init cred_init(void);
173174

174175
/*

‎kernel/cred.c‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <linux/security.h>
2020
#include <linux/binfmts.h>
2121
#include <linux/cn_proc.h>
22+
#include <linux/uidgid.h>
2223

2324
#if 0
2425
#define kdebug(FMT, ...) \
@@ -564,6 +565,60 @@ void revert_creds(const struct cred *old)
564565
}
565566
EXPORT_SYMBOL(revert_creds);
566567

568+
/**
569+
* cred_fscmp - Compare two credentials with respect to filesystem access.
570+
* @a: The first credential
571+
* @b: The second credential
572+
*
573+
* cred_cmp() will return zero if both credentials have the same
574+
* fsuid, fsgid, and supplementary groups. That is, if they will both
575+
* provide the same access to files based on mode/uid/gid.
576+
* If the credentials are different, then either -1 or 1 will
577+
* be returned depending on whether @a comes before or after @b
578+
* respectively in an arbitrary, but stable, ordering of credentials.
579+
*
580+
* Return: -1, 0, or 1 depending on comparison
581+
*/
582+
int cred_fscmp(const struct cred *a, const struct cred *b)
583+
{
584+
struct group_info *ga, *gb;
585+
int g;
586+
587+
if (a == b)
588+
return 0;
589+
if (uid_lt(a->fsuid, b->fsuid))
590+
return -1;
591+
if (uid_gt(a->fsuid, b->fsuid))
592+
return 1;
593+
594+
if (gid_lt(a->fsgid, b->fsgid))
595+
return -1;
596+
if (gid_gt(a->fsgid, b->fsgid))
597+
return 1;
598+
599+
ga = a->group_info;
600+
gb = b->group_info;
601+
if (ga == gb)
602+
return 0;
603+
if (ga == NULL)
604+
return -1;
605+
if (gb == NULL)
606+
return 1;
607+
if (ga->ngroups < gb->ngroups)
608+
return -1;
609+
if (ga->ngroups > gb->ngroups)
610+
return 1;
611+
612+
for (g = 0; g < ga->ngroups; g++) {
613+
if (gid_lt(ga->gid[g], gb->gid[g]))
614+
return -1;
615+
if (gid_gt(ga->gid[g], gb->gid[g]))
616+
return 1;
617+
}
618+
return 0;
619+
}
620+
EXPORT_SYMBOL(cred_fscmp);
621+
567622
/*
568623
* initialise the credentials stuff
569624
*/

0 commit comments

Comments
 (0)