Skip to content
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

pyodbc: initial integration #8347

Merged
merged 2 commits into from
Aug 30, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions projects/pyodbc/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Copyright 2022 Google LLC
#
# 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.
#
################################################################################

FROM gcr.io/oss-fuzz-base/base-builder-python
RUN apt-get update && apt-get install -y make autoconf automake libtool unixodbc-dev
RUN git clone --depth 1 https://github.com/mkleehammer/pyodbc
WORKDIR pyodbc
COPY build.sh *.py *.c $SRC/
25 changes: 25 additions & 0 deletions projects/pyodbc/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/bin/bash -eu
# Copyright 2022 Google LLC
#
# 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.
#
################################################################################

# Compile the fake odbc driver
clang -shared -o $OUT/fuzzodbc.so $SRC/fake_odbc_driver.c

python3 setup.py install
pip3 install .
for fuzzer in $(find $SRC -name 'fuzz_*.py'); do
LD_PRELOAD=$OUT/sanitizer_with_fuzzer.so ASAN_OPTIONS=detect_leaks=0 compile_python_fuzzer $fuzzer --add-data $OUT/fuzzodbc.so:.
done
35 changes: 35 additions & 0 deletions projects/pyodbc/fake_odbc_driver.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/* Copyright 2022 Google LLC
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.
*/

int SQLAllocHandle(long a1, long a2, long a3){
return 0;
}

int SQLDriverConnect(long a1, long a2, long a3, long a4, long a5, long a6, long a7, long a8) {
return 0;
}

int SQLSetConnectAttr(long a1, long a2, long a3, long a4) {
return 0;
}

int SQLExecDirectW(long a1, long a2, long a3) {
return 0;
}

int SQLRowCount(long a1, long a2) {
return 0;
}

int SQLNumResultCols(long a1, long a2) {
return 0;
}
61 changes: 61 additions & 0 deletions projects/pyodbc/fuzz_curs_exec.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#!/usr/bin/python3
# Copyright 2022 Google LLC
#
# 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.
"""Fuzzer that targets native code of pyodbc with a fake odbc driver"""
import os
import sys
import atheris
import pyodbc

@atheris.instrument_func
def fuzz_exec(data):
fdp = atheris.FuzzedDataProvider(data)
connstr = "DRIVER=FUZZ;"
s1 = fdp.ConsumeUnicodeNoSurrogates(50)
if "DRIVER" in s1:
return
cstr = connstr + s1
connection_obj = pyodbc.connect(cstr)
csr = connection_obj.cursor()
try:
csr.execute(fdp.ConsumeUnicodeNoSurrogates(20))
except Exception as e:
if "Invalid string or buffer length" in str(e):
pass
else:
raise e
return 0

@atheris.instrument_func
def TestOneInput(data):
try:
return fuzz_exec(data)
except SystemError:
return 0


def main():
# Write the odbcinst.ini file
dir_path = os.path.dirname(os.path.realpath(__file__))
with open("/etc/odbcinst.ini", "w") as f:
f.write("[FUZZ]\n")
f.write("Driver=%s/fuzzodbc.so\n"%(dir_path))

atheris.instrument_all()
atheris.Setup(sys.argv, TestOneInput, enable_python_coverage=True)
atheris.Fuzz()


if __name__ == "__main__":
main()
11 changes: 11 additions & 0 deletions projects/pyodbc/project.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
homepage: "https://github.com/mkleehammer/pyodbc"
language: python
main_repo: "https://github.com/mkleehammer/pyodbc"
fuzzing_engines:
- libfuzzer
sanitizers:
- address
- undefined
vendor_ccs:
- david@adalogics.com
- adam@adalogics.com