Skip to content

Commit ca6885e

Browse files
authored
Merge pull request #5998 from larsewi/str2id-3.24.x
ENT-13551: Fixed potential buffer overflow when converting strings to GIDs/UIDs (3.24.x)
2 parents 9726786 + f80d278 commit ca6885e

File tree

5 files changed

+15
-17
lines changed

5 files changed

+15
-17
lines changed

cf-agent/cf-agent.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1667,7 +1667,7 @@ static void CheckAgentAccess(const Rlist *list, const Policy *policy)
16671667

16681668
for (const Rlist *rp = list; rp != NULL; rp = rp->next)
16691669
{
1670-
if (Str2Uid(RlistScalarValue(rp), NULL, NULL) == uid)
1670+
if (Str2Uid(RlistScalarValue(rp), NULL, 0, NULL) == uid)
16711671
{
16721672
return;
16731673
}
@@ -1687,7 +1687,7 @@ static void CheckAgentAccess(const Rlist *list, const Policy *policy)
16871687
bool access = false;
16881688
for (const Rlist *rp2 = ACCESSLIST; rp2 != NULL; rp2 = rp2->next)
16891689
{
1690-
if (Str2Uid(RlistScalarValue(rp2), NULL, NULL) == sb.st_uid)
1690+
if (Str2Uid(RlistScalarValue(rp2), NULL, 0, NULL) == sb.st_uid)
16911691
{
16921692
access = true;
16931693
break;

libpromises/conversion.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -988,7 +988,7 @@ UidList *Rlist2UidList(Rlist *uidnames, const Promise *pp)
988988
for (rp = uidnames; rp != NULL; rp = rp->next)
989989
{
990990
username[0] = '\0';
991-
uid = Str2Uid(RlistScalarValue(rp), username, pp);
991+
uid = Str2Uid(RlistScalarValue(rp), username, sizeof(username), pp);
992992
AddSimpleUidItem(&uidlist, uid, username);
993993
}
994994

@@ -1049,7 +1049,7 @@ GidList *Rlist2GidList(Rlist *gidnames, const Promise *pp)
10491049
for (rp = gidnames; rp != NULL; rp = rp->next)
10501050
{
10511051
groupname[0] = '\0';
1052-
gid = Str2Gid(RlistScalarValue(rp), groupname, pp);
1052+
gid = Str2Gid(RlistScalarValue(rp), groupname, sizeof(groupname), pp);
10531053
AddSimpleGidItem(&gidlist, gid, groupname);
10541054
}
10551055

@@ -1063,7 +1063,7 @@ GidList *Rlist2GidList(Rlist *gidnames, const Promise *pp)
10631063

10641064
/*********************************************************************/
10651065

1066-
uid_t Str2Uid(const char *uidbuff, char *usercopy, const Promise *pp)
1066+
uid_t Str2Uid(const char *uidbuff, char *usercopy, size_t copy_size, const Promise *pp)
10671067
{
10681068
if (StringEqual(uidbuff, "*"))
10691069
{
@@ -1126,7 +1126,7 @@ uid_t Str2Uid(const char *uidbuff, char *usercopy, const Promise *pp)
11261126
{
11271127
if (usercopy != NULL)
11281128
{
1129-
strcpy(usercopy, uidbuff);
1129+
strlcpy(usercopy, uidbuff, copy_size);
11301130
}
11311131
}
11321132
else
@@ -1142,7 +1142,7 @@ uid_t Str2Uid(const char *uidbuff, char *usercopy, const Promise *pp)
11421142

11431143
/*********************************************************************/
11441144

1145-
gid_t Str2Gid(const char *gidbuff, char *groupcopy, const Promise *pp)
1145+
gid_t Str2Gid(const char *gidbuff, char *groupcopy, size_t copy_size, const Promise *pp)
11461146
{
11471147
if (StringEqual(gidbuff, "*"))
11481148
{
@@ -1169,7 +1169,7 @@ gid_t Str2Gid(const char *gidbuff, char *groupcopy, const Promise *pp)
11691169
{
11701170
if (groupcopy != NULL)
11711171
{
1172-
strcpy(groupcopy, gidbuff);
1172+
strlcpy(groupcopy, gidbuff, copy_size);
11731173
}
11741174
}
11751175
else

libpromises/conversion.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ void GidListDestroy(GidList *gids);
8383
UidList *Rlist2UidList(Rlist *uidnames, const Promise *pp);
8484
GidList *Rlist2GidList(Rlist *gidnames, const Promise *pp);
8585
#ifndef __MINGW32__
86-
uid_t Str2Uid(const char *uidbuff, char *copy, const Promise *pp);
87-
gid_t Str2Gid(const char *gidbuff, char *copy, const Promise *pp);
86+
uid_t Str2Uid(const char *uidbuff, char *copy, size_t copy_size, const Promise *pp);
87+
gid_t Str2Gid(const char *gidbuff, char *copy, size_t copy_size, const Promise *pp);
8888
#endif /* !__MINGW32__ */
8989

9090
#endif

libpromises/evalfunction.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1121,7 +1121,7 @@ static FnCallResult FnCallGetUserInfo(ARG_UNUSED EvalContext *ctx, ARG_UNUSED co
11211121
char *arg = RlistScalarValue(finalargs);
11221122
if (StringIsNumeric(arg))
11231123
{
1124-
uid_t uid = Str2Uid(arg, NULL, NULL);
1124+
uid_t uid = Str2Uid(arg, NULL, 0, NULL);
11251125
if (uid == CF_SAME_OWNER) // user "*"
11261126
{
11271127
uid = getuid();
@@ -8129,7 +8129,7 @@ FnCallResult FnCallUserExists(ARG_UNUSED EvalContext *ctx, ARG_UNUSED const Poli
81298129

81308130
if (StringIsNumeric(arg))
81318131
{
8132-
uid_t uid = Str2Uid(arg, NULL, NULL);
8132+
uid_t uid = Str2Uid(arg, NULL, 0, NULL);
81338133
if (uid == CF_SAME_OWNER || uid == CF_UNKNOWN_OWNER)
81348134
{
81358135
return FnFailure();
@@ -8156,7 +8156,7 @@ FnCallResult FnCallGroupExists(ARG_UNUSED EvalContext *ctx, ARG_UNUSED const Pol
81568156

81578157
if (StringIsNumeric(arg))
81588158
{
8159-
gid_t gid = Str2Gid(arg, NULL, NULL);
8159+
gid_t gid = Str2Gid(arg, NULL, 0, NULL);
81608160
if (gid == CF_SAME_GROUP || gid == CF_UNKNOWN_GROUP)
81618161
{
81628162
return FnFailure();

libpromises/policy.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2931,7 +2931,6 @@ uid_t PromiseGetConstraintAsUid(const EvalContext *ctx, const char *lval, const
29312931
uid_t PromiseGetConstraintAsUid(const EvalContext *ctx, const char *lval, const Promise *pp)
29322932
{
29332933
int retval = CF_SAME_OWNER;
2934-
char buffer[CF_MAXVARSIZE];
29352934

29362935
const Constraint *cp = PromiseGetConstraint(pp, lval);
29372936
if (cp)
@@ -2945,7 +2944,7 @@ uid_t PromiseGetConstraintAsUid(const EvalContext *ctx, const char *lval, const
29452944
FatalError(ctx, "Aborted");
29462945
}
29472946

2948-
retval = Str2Uid((char *) cp->rval.item, buffer, pp);
2947+
retval = Str2Uid((char *) cp->rval.item, NULL, 0, pp);
29492948
}
29502949

29512950
return retval;
@@ -2973,7 +2972,6 @@ gid_t PromiseGetConstraintAsGid(const EvalContext *ctx, char *lval, const Promis
29732972
gid_t PromiseGetConstraintAsGid(const EvalContext *ctx, char *lval, const Promise *pp)
29742973
{
29752974
int retval = CF_SAME_GROUP;
2976-
char buffer[CF_MAXVARSIZE];
29772975

29782976
const Constraint *cp = PromiseGetConstraint(pp, lval);
29792977
if (cp)
@@ -2987,7 +2985,7 @@ gid_t PromiseGetConstraintAsGid(const EvalContext *ctx, char *lval, const Promis
29872985
FatalError(ctx, "Aborted");
29882986
}
29892987

2990-
retval = Str2Gid((char *) cp->rval.item, buffer, pp);
2988+
retval = Str2Gid((char *) cp->rval.item, NULL, 0, pp);
29912989
}
29922990

29932991
return retval;

0 commit comments

Comments
 (0)