Skip to content

Commit

Permalink
selinux: Streamline type determination in security_compute_sid
Browse files Browse the repository at this point in the history
[ Upstream commit da2d41308c4206b966d77dcb77fb838d94244352 ]

Simplifies the logic for determining the security context type in
security_compute_sid, enhancing readability and efficiency.

Consolidates default type assignment logic next to type transition
checks, removing redundancy and improving code flow.

Signed-off-by: Canfeng Guo <[email protected]>
Signed-off-by: Paul Moore <[email protected]>
[ Backport from v6.12-rc1 ]
Signed-off-by: WangYuli <[email protected]>
  • Loading branch information
Canfeng Guo authored and Avenger-285714 committed Oct 7, 2024
1 parent 6826d96 commit f0701b8
Showing 1 changed file with 19 additions and 17 deletions.
36 changes: 19 additions & 17 deletions security/selinux/ss/services.c
Original file line number Diff line number Diff line change
Expand Up @@ -1794,22 +1794,9 @@ static int security_compute_sid(u32 ssid,
newcontext.role = OBJECT_R_VAL;
}

/* Set the type to default values. */
if (cladatum && cladatum->default_type == DEFAULT_SOURCE) {
newcontext.type = scontext->type;
} else if (cladatum && cladatum->default_type == DEFAULT_TARGET) {
newcontext.type = tcontext->type;
} else {
if ((tclass == policydb->process_class) || sock) {
/* Use the type of process. */
newcontext.type = scontext->type;
} else {
/* Use the type of the related object. */
newcontext.type = tcontext->type;
}
}

/* Look for a type transition/member/change rule. */
/* Set the type.
* Look for a type transition/member/change rule.
*/
avkey.source_type = scontext->type;
avkey.target_type = tcontext->type;
avkey.target_class = tclass;
Expand All @@ -1827,9 +1814,24 @@ static int security_compute_sid(u32 ssid,
}
}

/* If a permanent rule is found, use the type from
* the type transition/member/change rule. Otherwise,
* set the type to its default values.
*/
if (avnode) {
/* Use the type from the type transition/member/change rule. */
newcontext.type = avnode->datum.u.data;
} else if (cladatum && cladatum->default_type == DEFAULT_SOURCE) {
newcontext.type = scontext->type;
} else if (cladatum && cladatum->default_type == DEFAULT_TARGET) {
newcontext.type = tcontext->type;
} else {
if ((tclass == policydb->process_class) || sock) {
/* Use the type of process. */
newcontext.type = scontext->type;
} else {
/* Use the type of the related object. */
newcontext.type = tcontext->type;
}
}

/* if we have a objname this is a file trans check so check those rules */
Expand Down

0 comments on commit f0701b8

Please sign in to comment.