Rajaram-Regupathy / libtypec

“libtypec” is aimed to provide a generic interface abstracting all platform complexity for user space to develop tools for efficient USB-C port management. The library can also enable development of diagnostic and debug tools to debug system issues around USB-C/USB PD topology.
34 stars 4 forks source link

potential time of check of use (TOCTOU) issue with lstat on path_str and then opening the directory on path_str in ibtypec_sysfs_get_pdos_ops #25

Closed ColinIanKing closed 5 months ago

ColinIanKing commented 1 year ago

In function libtypec_sysfs_get_pdos_ops there is an lstat check followed by a later opendir on path_str. Stringent security code checkers complain that the directory may have changed (e.g. renamed or replaced) between the lstat and opendir. Personally I don't think this specific case needs to be worried about, but one could actually remove the lstat because if the path does not exist the opendir() will certainly catch this failure case. So it may be worth doing:

typec_path = opendir(path_str);
if (!typec_path) {
        printf("Incorrect connector number : failed to open, %s", path_str);
        return -1;
} 

and removing the lstat(). Also should error messages be fprinting to stderr?

Rajaram-Regupathy commented 10 months ago

Added for argument checking for better error reporting. Will analyze more

Rajaram-Regupathy commented 5 months ago

fixed