Skip to content

Commit 348b46a

Browse files
committed
Fixed potential buffer overflow when converting strings to GIDs/UIDs
Ticket: ENT-13551 Changelog: Title Signed-off-by: Lars Erik Wik <lars.erik.wik@northern.tech>
1 parent 3c1612d commit 348b46a

File tree

5 files changed

+16
-18
lines changed

5 files changed

+16
-18
lines changed

cf-agent/cf-agent.c

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

17671767
for (const Rlist *rp = list; rp != NULL; rp = rp->next)
17681768
{
1769-
if (Str2Uid(RlistScalarValue(rp), NULL, NULL) == uid)
1769+
if (Str2Uid(RlistScalarValue(rp), NULL, 0, NULL) == uid)
17701770
{
17711771
return;
17721772
}
@@ -1786,7 +1786,7 @@ static void CheckAgentAccess(const Rlist *list, const Policy *policy)
17861786
bool access = false;
17871787
for (const Rlist *rp2 = ACCESSLIST; rp2 != NULL; rp2 = rp2->next)
17881788
{
1789-
if (Str2Uid(RlistScalarValue(rp2), NULL, NULL) == sb.st_uid)
1789+
if (Str2Uid(RlistScalarValue(rp2), NULL, 0, NULL) == sb.st_uid)
17901790
{
17911791
access = true;
17921792
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: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1542,7 +1542,7 @@ static FnCallResult FnCallGetUserInfo(ARG_UNUSED EvalContext *ctx, ARG_UNUSED co
15421542
char *arg = RlistScalarValue(finalargs);
15431543
if (StringIsNumeric(arg))
15441544
{
1545-
uid_t uid = Str2Uid(arg, NULL, NULL);
1545+
uid_t uid = Str2Uid(arg, NULL, 0, NULL);
15461546
if (uid == CF_SAME_OWNER) // user "*"
15471547
{
15481548
uid = getuid();
@@ -1592,7 +1592,7 @@ static FnCallResult FnCallGetGroupInfo(ARG_UNUSED EvalContext *ctx, ARG_UNUSED c
15921592
char *arg = RlistScalarValue(finalargs);
15931593
if (StringIsNumeric(arg))
15941594
{
1595-
gid_t gid = Str2Gid(arg, NULL, NULL);
1595+
gid_t gid = Str2Gid(arg, NULL, 0, NULL);
15961596
if (gid == CF_SAME_GROUP) // user "*"
15971597
{
15981598
gid = getgid();
@@ -9261,7 +9261,7 @@ FnCallResult FnCallUserExists(ARG_UNUSED EvalContext *ctx, ARG_UNUSED const Poli
92619261

92629262
if (StringIsNumeric(arg))
92639263
{
9264-
uid_t uid = Str2Uid(arg, NULL, NULL);
9264+
uid_t uid = Str2Uid(arg, NULL, 0, NULL);
92659265
if (uid == CF_SAME_OWNER || uid == CF_UNKNOWN_OWNER)
92669266
{
92679267
return FnFailure();
@@ -9288,7 +9288,7 @@ FnCallResult FnCallGroupExists(ARG_UNUSED EvalContext *ctx, ARG_UNUSED const Pol
92889288

92899289
if (StringIsNumeric(arg))
92909290
{
9291-
gid_t gid = Str2Gid(arg, NULL, NULL);
9291+
gid_t gid = Str2Gid(arg, NULL, 0, NULL);
92929292
if (gid == CF_SAME_GROUP || gid == CF_UNKNOWN_GROUP)
92939293
{
92949294
return FnFailure();

libpromises/policy.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2964,7 +2964,6 @@ uid_t PromiseGetConstraintAsUid(const EvalContext *ctx, const char *lval, const
29642964
uid_t PromiseGetConstraintAsUid(const EvalContext *ctx, const char *lval, const Promise *pp)
29652965
{
29662966
int retval = CF_SAME_OWNER;
2967-
char buffer[CF_MAXVARSIZE];
29682967

29692968
const Constraint *cp = PromiseGetConstraint(pp, lval);
29702969
if (cp)
@@ -2978,7 +2977,7 @@ uid_t PromiseGetConstraintAsUid(const EvalContext *ctx, const char *lval, const
29782977
FatalError(ctx, "Aborted");
29792978
}
29802979

2981-
retval = Str2Uid((char *) cp->rval.item, buffer, pp);
2980+
retval = Str2Uid((char *) cp->rval.item, NULL, 0, pp);
29822981
}
29832982

29842983
return retval;
@@ -3006,7 +3005,6 @@ gid_t PromiseGetConstraintAsGid(const EvalContext *ctx, char *lval, const Promis
30063005
gid_t PromiseGetConstraintAsGid(const EvalContext *ctx, char *lval, const Promise *pp)
30073006
{
30083007
int retval = CF_SAME_GROUP;
3009-
char buffer[CF_MAXVARSIZE];
30103008

30113009
const Constraint *cp = PromiseGetConstraint(pp, lval);
30123010
if (cp)
@@ -3020,7 +3018,7 @@ gid_t PromiseGetConstraintAsGid(const EvalContext *ctx, char *lval, const Promis
30203018
FatalError(ctx, "Aborted");
30213019
}
30223020

3023-
retval = Str2Gid((char *) cp->rval.item, buffer, pp);
3021+
retval = Str2Gid((char *) cp->rval.item, NULL, 0, pp);
30243022
}
30253023

30263024
return retval;

0 commit comments

Comments
 (0)