-
Notifications
You must be signed in to change notification settings - Fork 33
feat: add path traversal bug detector #419
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e2f4bb8
bug detectors: add path traversal
fb82619
bug detectors: fix error stack and the error message of the finding
oetr 1f7e62c
bug detectors: track hooks of built-in modules
oetr 9e18931
bug detectors: builtin hooks use proper callSiteIDs for hookIDs
oetr 7b8a285
bug detectors: split up tests into several files
oetr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* Copyright 2023 Code Intelligence GmbH | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-var-requires | ||
const JSZip = require("jszip"); | ||
// eslint-disable-next-line @typescript-eslint/no-var-requires | ||
const path = require("path"); | ||
|
||
/** | ||
* This demonstrates the path traversal bug detector on a vulnerable version of jszip. | ||
* @param { Buffer } data | ||
*/ | ||
module.exports.fuzz = function (data) { | ||
// Parse the buffer into a JSZip object. The buffer might have been obtained from an http-request. | ||
// See https://stuk.github.io/jszip/documentation/howto/read_zip.html for some examples. | ||
return ( | ||
JSZip.loadAsync(data) | ||
.then((zip) => { | ||
for (const file in zip.files) { | ||
// We might want to extract the file from the zip archive and write it to disk. | ||
// The loadAsync function should have sanitized the path already. | ||
// Here we only construct the absolute path and trigger the path traversal bug. | ||
// This issue was fixed in jszip 3.8.0. | ||
path.join(__dirname, file); | ||
} | ||
}) | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
.catch(() => { | ||
/* ignore broken zip files */ | ||
}) | ||
); | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"name": "custom-hooks-bd", | ||
"version": "1.0.0", | ||
"main": "fuzz.js", | ||
"license": "ISC", | ||
"dependencies": { | ||
"jszip": "3.7.1" | ||
}, | ||
"scripts": { | ||
"fuzz": "jazzer fuzz -i fuzz.js -i jszip corpus -- -runs=10000000 -print_final_stats=1 -use_value_profile=1 -max_len=600 -seed=123456789", | ||
"dryRun": "jazzer fuzz --sync -x Error -- -runs=100000 -seed=123456789" | ||
oetr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}, | ||
"devDependencies": { | ||
"@jazzer.js/core": "file:../../packages/core" | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
{ | ||
"fuzzerOptions": ["-use_value_profile=1", "-runs=100000"], | ||
"includes": ["jazzer.js"], | ||
"timeout": 1000 | ||
"timeout": 1000, | ||
"disableBugDetectors": ['path-traversal'] | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,201 @@ | ||
/* | ||
* Copyright 2023 Code Intelligence GmbH | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { reportFinding } from "../findings"; | ||
import { guideTowardsContainment } from "@jazzer.js/fuzzer"; | ||
import { callSiteId, registerBeforeHook } from "@jazzer.js/hooking"; | ||
|
||
/** | ||
* Importing this file adds "before-hooks" for all functions in the built-in `fs`, `fs/promises`, and `path` module and guides | ||
* the fuzzer towards the uniquely chosen `goal` string `"../../jaz_zer"`. If the goal is found in the first argument | ||
* of any hooked function, a `Finding` is reported. | ||
*/ | ||
const goal = "../../jaz_zer"; | ||
const modulesToHook = [ | ||
{ | ||
moduleName: "fs", | ||
functionNames: [ | ||
"access", | ||
"accessSync", | ||
"appendFile", | ||
"appendFileSync", | ||
"chmod", | ||
"chown", | ||
"chownSync", | ||
"chmodSync", | ||
"createReadStream", | ||
"createWriteStream", | ||
"exists", | ||
"existsSync", | ||
"lchmod", | ||
"lchmodSync", | ||
"lchown", | ||
"lchownSync", | ||
"lstat", | ||
"lstatSync", | ||
"lutimes", | ||
"lutimesSync", | ||
"mkdir", | ||
"mkdirSync", | ||
"open", | ||
"opendir", | ||
"opendirSync", | ||
"openAsBlob", | ||
"openSync", | ||
"readFile", | ||
"readFileSync", | ||
"readlink", | ||
"readlinkSync", | ||
"readdir", | ||
"readdirSync", | ||
"realpath", | ||
"realpathSync", | ||
"rm", | ||
"rmSync", | ||
"rmdir", | ||
"rmdirSync", | ||
"stat", | ||
"statfs", | ||
"statfsSync", | ||
"statSync", | ||
"truncate", | ||
"truncateSync", | ||
"unlink", | ||
"unlinkSync", | ||
"unwatchFile", | ||
"utimes", | ||
"utimesSync", | ||
"watch", | ||
"watchFile", | ||
"writeFile", | ||
"writeFileSync", | ||
], | ||
}, | ||
{ | ||
moduleName: "fs/promises", | ||
functionNames: [ | ||
"access", | ||
"appendFile", | ||
"chmod", | ||
"chown", | ||
"lchmod", | ||
"lchown", | ||
"lstat", | ||
"lutimes", | ||
"mkdir", | ||
"open", | ||
"opendir", | ||
"readFile", | ||
"readlink", | ||
"readdir", | ||
"realpath", | ||
"rm", | ||
"rmdir", | ||
"stat", | ||
"statfs", | ||
"truncate", | ||
"unlink", | ||
"utimes", | ||
"watch", | ||
"writeFile", | ||
], | ||
}, | ||
// path.join() can have any number of strings as inputs. Internally, it uses path.normalize(), which we hook here. | ||
{ | ||
moduleName: "path", | ||
functionNames: ["normalize", "resolve"], | ||
}, | ||
]; | ||
|
||
for (const module of modulesToHook) { | ||
for (const functionName of module.functionNames) { | ||
const beforeHook = ( | ||
thisPtr: unknown, | ||
params: unknown[], | ||
hookId: number | ||
) => { | ||
if (params === undefined || params.length === 0) { | ||
return; | ||
} | ||
// The first argument of the original function is typically | ||
// a path or a file name. | ||
const firstArgument = params[0] as string; | ||
if (firstArgument.includes(goal)) { | ||
reportFinding( | ||
`Path Traversal in ${functionName}(): called with '${firstArgument}'` | ||
); | ||
} | ||
guideTowardsContainment(firstArgument, goal, hookId); | ||
}; | ||
|
||
registerBeforeHook(functionName, module.moduleName, false, beforeHook); | ||
} | ||
} | ||
|
||
// Some functions have two arguments that can be used for path traversal. | ||
const functionsWithTwoTargets = [ | ||
oetr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
moduleName: "fs/promises", | ||
functionNames: ["copyFile", "cp", "link", "rename", "symlink"], | ||
}, | ||
{ | ||
moduleName: "fs", | ||
functionNames: [ | ||
"copyFile", | ||
"copyFileSync", | ||
"cp", | ||
"cpSync", | ||
"link", | ||
"linkSync", | ||
"rename", | ||
"renameSync", | ||
"symlink", | ||
"symlinkSync", | ||
], | ||
}, | ||
]; | ||
|
||
for (const module of functionsWithTwoTargets) { | ||
for (const functionName of module.functionNames) { | ||
const makeBeforeHook = (extraHookId: number) => { | ||
return (thisPtr: unknown, params: unknown[], hookId: number) => { | ||
if (params === undefined || params.length < 2) { | ||
return; | ||
} | ||
// The first two arguments are paths. | ||
const firstArgument = params[0] as string; | ||
const secondArgument = params[1] as string; | ||
if (firstArgument.includes(goal) || secondArgument.includes(goal)) { | ||
reportFinding( | ||
`Path Traversal in ${functionName}(): called with '${firstArgument}'` + | ||
` and '${secondArgument}'` | ||
); | ||
} | ||
guideTowardsContainment(firstArgument, goal, hookId); | ||
// We don't want to confuse the fuzzer guidance with the same hookId for both function arguments. | ||
// Therefore, we use an extra hookId for the second argument. | ||
guideTowardsContainment(secondArgument, goal, extraHookId); | ||
}; | ||
}; | ||
|
||
registerBeforeHook( | ||
functionName, | ||
module.moduleName, | ||
false, | ||
makeBeforeHook(callSiteId(functionName, module.moduleName, "secondId")) | ||
); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.