From 6b74be53ae83d255e3513e336b8ed9efbd52a182 Mon Sep 17 00:00:00 2001 From: Adeeb Shihadeh Date: Mon, 25 Jul 2022 13:31:39 -0700 Subject: [PATCH] check vin validity (#25199) --- selfdrive/car/car_helpers.py | 4 ++-- selfdrive/car/vin.py | 6 ++++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/selfdrive/car/car_helpers.py b/selfdrive/car/car_helpers.py index 1a9a5f50f3..aa4b37d572 100644 --- a/selfdrive/car/car_helpers.py +++ b/selfdrive/car/car_helpers.py @@ -7,7 +7,7 @@ from common.basedir import BASEDIR from system.version import is_comma_remote, is_tested_branch from selfdrive.car.interfaces import get_interface_attr from selfdrive.car.fingerprints import eliminate_incompatible_cars, all_legacy_fingerprint_cars -from selfdrive.car.vin import get_vin, VIN_UNKNOWN +from selfdrive.car.vin import get_vin, is_valid_vin, VIN_UNKNOWN from selfdrive.car.fw_versions import get_fw_versions_ordered, match_fw_to_car, get_present_ecus from system.swaglog import cloudlog import cereal.messaging as messaging @@ -106,7 +106,7 @@ def fingerprint(logcan, sendcan): vin, vin_rx_addr = VIN_UNKNOWN, 0 exact_fw_match, fw_candidates, car_fw = True, set(), [] - if len(vin) != 17: + if not is_valid_vin(vin): cloudlog.event("Malformed VIN", vin=vin, error=True) vin = VIN_UNKNOWN cloudlog.warning("VIN %s", vin) diff --git a/selfdrive/car/vin.py b/selfdrive/car/vin.py index 007c10e772..5ace68649b 100755 --- a/selfdrive/car/vin.py +++ b/selfdrive/car/vin.py @@ -1,4 +1,5 @@ #!/usr/bin/env python3 +import re import struct import traceback @@ -15,6 +16,11 @@ UDS_VIN_REQUEST = bytes([uds.SERVICE_TYPE.READ_DATA_BY_IDENTIFIER]) + struct.pac UDS_VIN_RESPONSE = bytes([uds.SERVICE_TYPE.READ_DATA_BY_IDENTIFIER + 0x40]) + struct.pack("!H", uds.DATA_IDENTIFIER_TYPE.VIN) VIN_UNKNOWN = "0" * 17 +VIN_RE = "[A-HJ-NPR-Z0-9]{17}" + + +def is_valid_vin(vin: str): + return re.fullmatch(VIN_RE, vin) is not None def get_vin(logcan, sendcan, bus, timeout=0.1, retry=5, debug=False):