Add files via upload

This commit is contained in:
ihsinme
2021-10-25 14:47:25 +03:00
committed by GitHub
parent 5709365c0f
commit 1dacd2ea76
3 changed files with 101 additions and 0 deletions

View File

@@ -0,0 +1,10 @@
...
FILE *fp = fopen(filename,"w"); // BAD
...
umask(S_IXUSR|S_IRWXG|S_IRWXO);
FILE *fp;
fp = fopen(filename,"w"); // GOOD
chmod(filename,S_IRUSR|S_IWUSR);
fprintf(fp,"%s\n","data to file");
fclose(fp);
...

View File

@@ -0,0 +1,24 @@
<!DOCTYPE qhelp PUBLIC
"-//Semmle//qhelp//EN"
"qhelp.dtd">
<qhelp>
<overview>
<p>Finding for places to work with files without restrictions on access rights.</p>
</overview>
<example>
<p>The following example demonstrates erroneous and fixed methods for working with files.</p>
<sample src="ExposureSensitiveInformationUnauthorizedActor.cpp" />
</example>
<references>
<li>
CERT C Coding Standard:
<a href="https://wiki.sei.cmu.edu/confluence/display/c/FIO06-C.+Create+files+with+appropriate+access+permissions">FIO06-C. Create files with appropriate access permissions</a>.
</li>
</references>
</qhelp>

View File

@@ -0,0 +1,67 @@
/**
* @name Find work with the file without setting permissions.
* @description Lack of restriction on file access rights can be unsafe.
* @kind problem
* @id cpp/work-with-file-without-permissions-rights
* @problem.severity warning
* @precision medium
* @tags correctness
* maintainability
* security
* external/cwe/cwe-200
* external/cwe/cwe-264
*/
import cpp
import semmle.code.cpp.valuenumbering.GlobalValueNumbering
/** Holds for a function `f` that has an argument at index `apos` used to read the file. */
predicate numberArgumentRead(Function f, int apos) {
f.hasGlobalOrStdName("fgets") and apos = 2
or
f.hasGlobalOrStdName("fread") and apos = 3
or
f.hasGlobalOrStdName("read") and apos = 0
or
f.hasGlobalOrStdName("fscanf") and apos = 0
}
/** Holds for a function `f` that has an argument at index `apos` used to write to file */
predicate numberArgumentWrite(Function f, int apos) {
f.hasGlobalOrStdName("fprintf") and apos = 0
or
f.hasGlobalOrStdName("fputs") and apos = 1
or
f.hasGlobalOrStdName("write") and apos = 0
or
f.hasGlobalOrStdName("fwrite") and apos = 3
or
f.hasGlobalOrStdName("fflush") and apos = 0
}
from FunctionCall fc
where
(
fc.getTarget().hasGlobalOrStdName("fopen") or
fc.getTarget().hasGlobalOrStdName("open")
) and
fc.getNumberOfArguments() = 2 and
exists(FunctionCall fctmp, int i |
numberArgumentWrite(fctmp.getTarget(), i) and
globalValueNumber(fc) = globalValueNumber(fctmp.getArgument(i))
) and
not exists(FunctionCall fctmp, int i |
numberArgumentRead(fctmp.getTarget(), i) and
globalValueNumber(fc) = globalValueNumber(fctmp.getArgument(i))
) and
not exists(FunctionCall fctmp |
fctmp.getTarget().hasGlobalOrStdName("umask") or
fctmp.getTarget().hasGlobalOrStdName("fchmod") or
fctmp.getTarget().hasGlobalOrStdName("chmod")
) and
not exists(FunctionCall fctmp |
fctmp.getTarget().hasGlobalOrStdName("fdopen") and
fctmp.getNumberOfArguments() = 3 and
globalValueNumber(fctmp) = globalValueNumber(fc.getArgument(0))
)
select fc, "You may have forgotten to restrict access rights when working with a file."