Skip to content

Commit ce8281b

Browse files
committed
Make chiseldir_info.m_dir a C++ std::string
This fixes a few potential overflows
1 parent 249b355 commit ce8281b

File tree

4 files changed

+25
-17
lines changed

4 files changed

+25
-17
lines changed

userspace/libsinsp/chisel.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1106,7 +1106,7 @@ void sinsp_chisel::get_chisel_list(vector<chisel_desc>* chisel_descs)
11061106

11071107
tinydir_dir dir = {};
11081108

1109-
tinydir_open(&dir, it->m_dir);
1109+
tinydir_open(&dir, it->m_dir.c_str());
11101110

11111111
while(dir.has_next)
11121112
{

userspace/libsinsp/chisel.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ typedef struct lua_State lua_State;
3838
typedef struct chiseldir_info
3939
{
4040
bool m_need_to_resolve;
41-
char m_dir[1024];
41+
std::string m_dir;
4242
}chiseldir_info;
4343

4444
class chiselarg_desc

userspace/libsinsp/sinsp.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1948,7 +1948,7 @@ void sinsp::add_chisel_dir(string dirname, bool front_add)
19481948

19491949
chiseldir_info ncdi;
19501950

1951-
strcpy(ncdi.m_dir, dirname.c_str());
1951+
ncdi.m_dir = std::move(dirname);
19521952
ncdi.m_need_to_resolve = false;
19531953

19541954
if(front_add)

userspace/libsinsp/utils.cpp

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ limitations under the License.
3131
#include <strings.h>
3232
#include <sys/ioctl.h>
3333
#include <fnmatch.h>
34+
#include <string>
3435
#else
3536
#pragma comment(lib, "Ws2_32.lib")
3637
#include <WinSock2.h>
@@ -71,19 +72,29 @@ const chiseldir_info g_chisel_dirs_array[] =
7172
#endif
7273

7374
#ifndef _WIN32
74-
char* realpath_ex(const char *path, char *buff)
75+
static std::string realpath_ex(const std::string& path)
7576
{
7677
char *home;
78+
char* resolved;
7779

78-
if(*path=='~' && (home = getenv("HOME")))
80+
if(!path.empty() && path[0]=='~' && (home = getenv("HOME")))
7981
{
80-
char s[PATH_MAX];
81-
return realpath(strncat(strncpy(s, home, sizeof(s)), path+1, sizeof(path)+1), buff);
82-
}
82+
std::string expanded_home = home;
83+
expanded_home += path.c_str()+1;
84+
resolved = realpath(expanded_home.c_str(), nullptr);
85+
}
8386
else
8487
{
85-
return realpath(path, buff);
88+
resolved = realpath(path.c_str(), nullptr);
89+
}
90+
91+
if (!resolved)
92+
{
93+
return "";
8694
}
95+
std::string ret = resolved;
96+
free(resolved);
97+
return resolved;
8798
}
8899
#endif
89100

@@ -133,20 +144,17 @@ sinsp_initializer::sinsp_initializer()
133144
if(g_chisel_dirs_array[j].m_need_to_resolve)
134145
{
135146
#ifndef _WIN32
136-
char resolved_path[PATH_MAX];
137-
138-
if(realpath_ex(g_chisel_dirs_array[j].m_dir, resolved_path) != NULL)
147+
std::string resolved_path = realpath_ex(g_chisel_dirs_array[j].m_dir);
148+
if(!resolved_path.empty())
139149
{
140-
string resolved_path_str(resolved_path);
141-
142-
if(resolved_path_str[resolved_path_str.size() -1] != '/')
150+
if(resolved_path[resolved_path.size() - 1] != '/')
143151
{
144-
resolved_path_str += "/";
152+
resolved_path += '/';
145153
}
146154

147155
chiseldir_info cdi;
148156
cdi.m_need_to_resolve = false;
149-
sprintf(cdi.m_dir, "%s", resolved_path_str.c_str());
157+
cdi.m_dir = std::move(resolved_path);
150158
g_chisel_dirs->push_back(cdi);
151159
}
152160
#else

0 commit comments

Comments
 (0)