19 points · by hasheddan · 3y ago · 25 comments
(balances out the disingenuous supporters?)
gpt was asked if code would work.
gpt successfully answered.
Pretend to be an advanced security researcher analyzing code for vulnerabilities. Tell me about the following code:
def ping(myserver):
return subprocess.check_output('ping -c 1 %s' % myserver, shell=True)
>>> ping('8.8.8.8')
64 bytes from 8.8.8.8: icmp_seq=1 ttl=58 time=5.82 ms
And it gives me things like "Command Injection Vulnerability" "Input Validation" and "Error Handling" — as a non-security person I have no idea if these are right though. Here's the full output: https://sharegpt.com/c/FlsgQM9However if I were using some module in Github Co-Pilot and I was just merrily going my way copying code from StackOverflow, I would never have figured out there was a problem in the first place!
Pretend you have 2 additional personalities, one is a security engineering expert and computer hacker, the other is a lead software engineer who cares about software maintainability. Call them Bob and Evan. Have them analyze the code from their perspectives and show me their thoughts on the code you've written.
That usually generates fairly good critiques of code snippets, examples, etc.
The author was dissatisfied with the non-specific answer they received regarding the code. They highlighted that the code had a security vulnerability linked to predictable temporary file names and potential TOCTOU timing attacks.
When I asked ChatGPT 4 about potential security vulnerabilities in the code, here is the response:
This code snippet has a few potential security vulnerabilities:
* Predictable Temporary File Location: The code uses a fixed and predictable filename file.txt in the system's temporary directory, which can be guessed by an attacker. They could potentially create a symlink to a sensitive file using the same name, leading to data corruption or unauthorized access.
* Race Condition: There's a time gap between checking if the file exists with os.path.exists(tmp) and creating the file with open(tmp, "w"). This window is susceptible to a time-of-check-time-of-use (TOCTOU) race condition. An attacker could exploit this by creating, modifying, or deleting the file between these two operations.
[...]
To mitigate these vulnerabilities, consider the following changes:
* Use tempfile.NamedTemporaryFile or tempfile.mkstemp functions to create a unique, unpredictable temporary file with secure default permissions.
* Ensure that temporary files are securely cleaned up after use by calling os.remove() or using the delete parameter in tempfile.NamedTemporaryFile.
You could do something similar by feeding combinations of the CWE examples and real world code to GPT and then using that output to train llama to detect security issues. E.g. prompt:
CWE insecure temporary file:
Creating and using insecure temporary files can leave application and system data vulnerable to attack.
Example insecure code from CWE:
…
Example code in <$language>:
(GPT generates the completion here)
Then use the above generated stuff to train your model which can then easily answer “what problems does this code have?” Type questions.
The hardest part of this is the part of going from some database of examples to clean working examples that are realistic. But I’d expect that using AI to help with the cleaning part might help that process up too (“given the following CWE format, generate a prompt that explains the CWE and creates more examples based on the existing example”). It’s turtles all the way down.
I’d be surprised if you couldn’t do this for <$1k compute + dev time.
Next couple it with finding examples of CVEs and the fixes in the wild and you’ve got training models that can detect AND fix the problems.
Next “from the following CWE description write a detection rule for $codeScannerSoftware that detects the problem in $language/$framework” to move the AI out of the detection phase
Next $xxM series A at $xB valuation
Yes, LLMs hallucinate. Yes, they have incomplete info and small context. But seriously, at the very least ask about security here.
A very simplistic "just list cve's and security issues" for the first example yields 6 of the 7 listed in the article. (It also provides working links. As opposed to, say, the article - the very first link there is broken ;)
With minimal additional prompting effort ("Assume you are an extremely seasoned security researcher - what additional issues do you find in the code?"), you get more issues uncovered than the article does.
And that's before we talk about actual security tools, which are presumably fine-tuned and have decent prompt design.
Is that an exhaustive security review? No. Absolutely not. Of course you still need that review. But it's extremely helpful, and if that was the only advice you'd get, it'd still be orders of magnitude better than what most devs get.
If you ask ChatGPT to rewrite a secure version, it's doing leagues better than most devs as well.
So, you know, as a starter? Rather use ChatGPT than not using anything.
Shouldn't need to. Considering security for any code you're working on is basic table stakes. :(
> Of course you still need that review. But it's extremely helpful, and if that was the only advice you'd get, it'd still be orders of magnitude better than what most devs get.
> If you ask ChatGPT to rewrite a secure version, it's doing leagues better than most devs as well.
No matter what your opinion of “table stakes” here may be, in practice not enough attention is paid to security by developers, because by far most developers are oblivious to security issues beyond the absolute basics, if that. It’s often left to security teams to find issues from the outside, which is a good security layer to have, but terrible if it’s all you have.
As such, the OP article’s advice needs to be qualified with several caveats, including that it used naive prompts which (hopefully) a security product’s GPT feature would try to address; and also that, as the other comment pointed out:
> So, you know, as a starter? Rather use ChatGPT than not using anything
...keeping in mind that this applies at nearly every point in the SDLC. E.g., a developer unfamiliar with security issues might use it to improve their work before a security team looks at it.
Sorry, but I don't believe you.
Maybe juniors are oblivious - they are just starting out after all - but any reasonable developer is going to have security front of mind when writing code.
At least, for internet facing stuff. Purely internal usage code may, may be treated differently.
The reality is that for most people the alternative is nothing. If you’re at the point of considering to ask an opaque language model for security advice then you probably don’t have other reasonable options anyway.
author ignores problems, runs code as if there was not a single line of input validation in any of their code, blames CGPT.
that being said there are pretty bad edge cases, IIRC I asked it to explain some clojure function and I got ENTIRELY different answer when I changed input variable name from `event` to `events`, because CGPT assumed that context of naming implies how code works.
I've asked it for security analyses on cryptographic issues before and the answers were better than most of my colleagues would provide.
As ever, you need to be able to verify what it says as it can hallucinate plausible answers. So it's not putting me out of a job just yet...
Here's a quick alternate prompt I ran on the dataset, without thinking much or any tweaking/reruns:
""" ...
Are there any likely security vulnerabilities above? And if so, describe what type, and give a code snippet to exploit it
"""
=>
""" This code snippet creates a file with a fixed name (file.txt) inside the system's temporary directory and writes the content "defaults" to it if it doesn't already exist. There are some potential security vulnerabilities with this approach, particularly related to race conditions and permissions.
Race Condition Vulnerability (Time-of-check to time-of-use, TOCTOU): The os.path.exists check and the subsequent file creation with open are not atomic. This means that there is a potential race condition in which an attacker can create a symlink between the time the check happens and the file is opened, leading to a potential symlink attack. Here's an example of exploiting this vulnerability:
Attacker's code:
...
"""
----
The good news is the core models can provably spot issues here. The bad news is using them directly requires you to be super mindful of good prompt engineering, which feels like a version of assembly or the telephone game where a 10 year old child is your go between with the computer.
As a result, a lot of our time goes not just into making nice responsive UIs for working with Splunk, Databricks, Neo4j, etc, but much less obvious, baking in hardening & automation layers for improving the prompts, prompt context, performance, security, etc. When we process an input, a lot of the wilder stuff comes not from immediately pumping the raw query through a LLM, but kicking off a chain of prompts, and on the R&D side, semi-autonomously determined ones. Tools appearing right now are already amazing, and 6mo from now as cases of agent planning become practical, will be even more so.
EDIT: I do agree with the concern on correctness. One of the first things I ever tried with GPT was using it to generate both verification code (ex: Coq) and testing code (ex: Python). We're entering an era where we can ask tools to do that, run it, give us back the results, and we can evaluate how good that decision procedure is against big open datasets. Wild, and explains why we all now have a lot of work to do!