fix: strncpy reads out of bounds #52

Merged
SirDifferential merged 1 commits from strncpy_oob_fix into master 2020-06-08 12:36:05 +02:00
SirDifferential commented 2020-05-18 08:03:18 +02:00 (Migrated from github.com)

There are several out of bounds strncpy calls. This commit fixes them. On gcc 8.3.0 the SDK does not compile without this fix and gives the error:

/home/gekko/Livox-SDK/sdk_core/src/device_manager.cpp: In member function ‘bool livox::DeviceManager::AddListeningDevice(const string&, livox::DeviceM
ode, uint8_t&)’:                                                                                                                                      
/home/gekko/Livox-SDK/sdk_core/src/device_manager.cpp:177:14: error: ‘char* strncpy(char*, const char*, size_t)’ specified bound 16 equals destination
 size [-Werror=stringop-truncation]                                                                                                                   
       strncpy(ite->info.broadcast_code, broadcast_code.c_str(), sizeof(ite->info.broadcast_code));                                              
       ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~                                                    
/home/gekko/Livox-SDK/sdk_core/src/device_manager.cpp:166:12: error: ‘char* strncpy(char*, const char*, size_t)’ specified bound 16 equals destination
 size [-Werror=stringop-truncation]                                                                       
     strncpy(devices_[kHubDefaultHandle].info.broadcast_code,                               
     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~                                                                                    
             broadcast_code.c_str(),                                                                                                                 
             ~~~~~~~~~~~~~~~~~~~~~~~                                                                                                                 
             sizeof(devices_[kHubDefaultHandle].info.broadcast_code));                                                                                
             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~                                                                                

among other errors of similar nature.

There are a few similar cases elsewhere, although they only produce warnings. I didn't investigate them in detail, but here are the warnings:

/home/gekko/Livox-SDK/sample/lidar/main.c:273:7: warning: ‘strncpy’ specified bound depends on the length of the source argument [-Wstringop-overflow=
]
       strncpy(sn_list, optarg, sizeof(char)*(strlen(optarg) + 1));
       ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/home/gekko/Livox-SDK/sample/lidar/main.c:273:46: note: length computed here
       strncpy(sn_list, optarg, sizeof(char)*(strlen(optarg) + 1));
                                              ^~~~~~~~~~~~~~

/home/gekko/Livox-SDK/sample/lidar_lvx_file/main.cpp: In function ‘int SetProgramOption(int, const char**)’:                                         
/home/gekko/Livox-SDK/sample/lidar_lvx_file/main.cpp:324:14: warning: ‘char* strncpy(char*, const char*, size_t)’ specified bound depends on the lengt
h of the source argument [-Wstringop-overflow=]
       strncpy(sn_list, optarg, sizeof(char)*(strlen(optarg) + 1));
       ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/home/gekko/Livox-SDK/sample/lidar_lvx_file/main.cpp:324:52: note: length computed here                                                              
       strncpy(sn_list, optarg, sizeof(char)*(strlen(optarg) + 1));
                                              ~~~~~~^~~~~~~~
In function ‘int SetProgramOption(int, const char**)’,
    inlined from ‘int main(int, const char**)’ at /home/gekko/Livox-SDK/sample_cc/lidar/main.cpp:127:23:
/home/gekko/Livox-SDK/sample_cc/lidar/main.cpp:80:16: warning: ‘char* strncpy(char*, const char*, size_t)’ specified bound depends on the length of th
e source argument [-Wstringop-overflow=]
         strncpy(sn_list, optarg, sizeof(char)*(strlen(optarg) + 1));
         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/home/gekko/Livox-SDK/sample_cc/lidar/main.cpp: In function ‘int main(int, const char**)’:
/home/gekko/Livox-SDK/sample_cc/lidar/main.cpp:80:54: note: length computed here
         strncpy(sn_list, optarg, sizeof(char)*(strlen(optarg) + 1));
                                                ~~~~~~^~~~~~~~
In function ‘int SetProgramOption(int, const char**)’,
    inlined from ‘int main(int, const char**)’ at /home/gekko/Livox-SDK/sample_cc/lidar_utc_sync/main.cpp:129:23:                                    
/home/gekko/Livox-SDK/sample_cc/lidar_utc_sync/main.cpp:82:16: warning: ‘char* strncpy(char*, const char*, size_t)’ specified bound depends on the length of the source argument [-Wstringop-overflow=]
         strncpy(sn_list, optarg, sizeof(char)*(strlen(optarg) + 1));
         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/home/gekko/Livox-SDK/sample_cc/lidar_utc_sync/main.cpp: In function ‘int main(int, const char**)’:                                                  
/home/gekko/Livox-SDK/sample_cc/lidar_utc_sync/main.cpp:82:54: note: length computed here                                                            
         strncpy(sn_list, optarg, sizeof(char)*(strlen(optarg) + 1));
                                                ~~~~~~^~~~~~~~
There are several out of bounds strncpy calls. This commit fixes them. On gcc 8.3.0 the SDK does not compile without this fix and gives the error: ``` /home/gekko/Livox-SDK/sdk_core/src/device_manager.cpp: In member function ‘bool livox::DeviceManager::AddListeningDevice(const string&, livox::DeviceM ode, uint8_t&)’: /home/gekko/Livox-SDK/sdk_core/src/device_manager.cpp:177:14: error: ‘char* strncpy(char*, const char*, size_t)’ specified bound 16 equals destination size [-Werror=stringop-truncation] strncpy(ite->info.broadcast_code, broadcast_code.c_str(), sizeof(ite->info.broadcast_code)); ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ /home/gekko/Livox-SDK/sdk_core/src/device_manager.cpp:166:12: error: ‘char* strncpy(char*, const char*, size_t)’ specified bound 16 equals destination size [-Werror=stringop-truncation] strncpy(devices_[kHubDefaultHandle].info.broadcast_code, ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ broadcast_code.c_str(), ~~~~~~~~~~~~~~~~~~~~~~~ sizeof(devices_[kHubDefaultHandle].info.broadcast_code)); ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ``` among other errors of similar nature. There are a few similar cases elsewhere, although they only produce warnings. I didn't investigate them in detail, but here are the warnings: ``` /home/gekko/Livox-SDK/sample/lidar/main.c:273:7: warning: ‘strncpy’ specified bound depends on the length of the source argument [-Wstringop-overflow= ] strncpy(sn_list, optarg, sizeof(char)*(strlen(optarg) + 1)); ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ /home/gekko/Livox-SDK/sample/lidar/main.c:273:46: note: length computed here strncpy(sn_list, optarg, sizeof(char)*(strlen(optarg) + 1)); ^~~~~~~~~~~~~~ /home/gekko/Livox-SDK/sample/lidar_lvx_file/main.cpp: In function ‘int SetProgramOption(int, const char**)’: /home/gekko/Livox-SDK/sample/lidar_lvx_file/main.cpp:324:14: warning: ‘char* strncpy(char*, const char*, size_t)’ specified bound depends on the lengt h of the source argument [-Wstringop-overflow=] strncpy(sn_list, optarg, sizeof(char)*(strlen(optarg) + 1)); ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ /home/gekko/Livox-SDK/sample/lidar_lvx_file/main.cpp:324:52: note: length computed here strncpy(sn_list, optarg, sizeof(char)*(strlen(optarg) + 1)); ~~~~~~^~~~~~~~ In function ‘int SetProgramOption(int, const char**)’, inlined from ‘int main(int, const char**)’ at /home/gekko/Livox-SDK/sample_cc/lidar/main.cpp:127:23: /home/gekko/Livox-SDK/sample_cc/lidar/main.cpp:80:16: warning: ‘char* strncpy(char*, const char*, size_t)’ specified bound depends on the length of th e source argument [-Wstringop-overflow=] strncpy(sn_list, optarg, sizeof(char)*(strlen(optarg) + 1)); ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ /home/gekko/Livox-SDK/sample_cc/lidar/main.cpp: In function ‘int main(int, const char**)’: /home/gekko/Livox-SDK/sample_cc/lidar/main.cpp:80:54: note: length computed here strncpy(sn_list, optarg, sizeof(char)*(strlen(optarg) + 1)); ~~~~~~^~~~~~~~ In function ‘int SetProgramOption(int, const char**)’, inlined from ‘int main(int, const char**)’ at /home/gekko/Livox-SDK/sample_cc/lidar_utc_sync/main.cpp:129:23: /home/gekko/Livox-SDK/sample_cc/lidar_utc_sync/main.cpp:82:16: warning: ‘char* strncpy(char*, const char*, size_t)’ specified bound depends on the length of the source argument [-Wstringop-overflow=] strncpy(sn_list, optarg, sizeof(char)*(strlen(optarg) + 1)); ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ /home/gekko/Livox-SDK/sample_cc/lidar_utc_sync/main.cpp: In function ‘int main(int, const char**)’: /home/gekko/Livox-SDK/sample_cc/lidar_utc_sync/main.cpp:82:54: note: length computed here strncpy(sn_list, optarg, sizeof(char)*(strlen(optarg) + 1)); ~~~~~~^~~~~~~~ ```
Livox-SDK (Migrated from github.com) approved these changes 2020-06-08 12:35:51 +02:00
This repo is archived. You cannot comment on pull requests.