CWE Rule 273
Description
Rule Description
The product attempts to drop privileges but does not check or incorrectly checks to see if the drop succeeded.
Polyspace Implementation
The rule checker checks for Privilege drop not verified.
Examples
This issue occurs when you relinquish privileges using functions such as
setuid
but do not verify that the
privileges were actually dropped before exiting your
function.
If privilege relinquishment fails, an attacker can regain elevated privileges and have more access to your program than intended. This security hole can cause unexpected behavior in your code if left open.
Before the end of scope, verify that the privileges that you dropped were actually dropped.
#define _BSD_SOURCE
#include <sys/types.h>
#include <unistd.h>
#include <grp.h>
#include <stdlib.h>
#define fatal_error() abort()
extern int need_more_privileges;
void missingprivilegedropcheck()
{
/* Code intended to run with elevated privileges */
/* Temporarily drop elevated privileges */
if (seteuid(getuid()) != 0) {
/* Handle error */
fatal_error();
}
/* Code intended to run with lower privileges */
if (need_more_privileges) {
/* Restore elevated privileges */
if (seteuid(0) != 0) {
/* Handle error */
fatal_error();
}
/* Code intended to run with elevated privileges */
}
/* ... */
/* Permanently drop elevated privileges */
if (setuid(getuid()) != 0) {
/* Handle error */
fatal_error();
}
/* Code intended to run with lower privileges */
} //Noncompliant
In this example, privileges are elevated and dropped to run code with the intended privilege level. When privileges are dropped, the privilege level before exiting the function body is not verified. A malicious attacker can regain their elevated privileges.
One possible correction is to use setuid
to
verify that the privileges were dropped.
#define _BSD_SOURCE
#include <sys/types.h>
#include <unistd.h>
#include <grp.h>
#include <stdlib.h>
#define fatal_error() abort()
extern int need_more_privileges;
void missingprivilegedropcheck()
{
/* Store the privileged ID for later verification */
uid_t privid = geteuid();
/* Code intended to run with elevated privileges */
/* Temporarily drop elevated privileges */
if (seteuid(getuid()) != 0) {
/* Handle error */
fatal_error();
}
/* Code intended to run with lower privileges */
if (need_more_privileges) {
/* Restore elevated Privileges */
if (seteuid(privid) != 0) {
/* Handle error */
fatal_error();
}
/* Code intended to run with elevated privileges */
}
/* ... */
/* Restore privileges if needed */
if (geteuid() != privid) {
if (seteuid(privid) != 0) {
/* Handle error */
fatal_error();
}
}
/* Permanently drop privileges */
if (setuid(getuid()) != 0) {
/* Handle error */
fatal_error();
}
if (setuid(0) != -1) {
/* Privileges can be restored, handle error */
fatal_error();
}
/* Code intended to run with lower privileges; */
}
Check Information
Category: Privilege Issues |
Version History
Introduced in R2024a
See Also
External Websites
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)