Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
T
Tor
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Benjamin J. Thompson
Tor
Commits
1118bd99
Commit
1118bd99
authored
11 years ago
by
Cristian Toader
Browse files
Options
Downloads
Patches
Plain Diff
switched from multiple mmap to one
parent
f93ba9a2
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/common/sandbox.c
+59
-30
59 additions, 30 deletions
src/common/sandbox.c
with
59 additions
and
30 deletions
src/common/sandbox.c
+
59
−
30
View file @
1118bd99
...
...
@@ -678,36 +678,65 @@ sandbox_intern_string(const char *param)
return
param
;
}
static
char
*
prot_strdup
(
char
*
str
)
{
int
param_size
=
0
;
char
*
res
=
NULL
;
static
int
prot_strings
(
sandbox_cfg_t
*
cfg
)
{
int
ret
=
0
;
int
pr_mem_size
=
0
,
pr_mem_left
=
0
;
char
*
pr_mem_next
=
NULL
,
*
pr_mem_base
;
sandbox_cfg_t
*
el
=
NULL
;
// get total number of bytes required to mmap
for
(
el
=
cfg
;
el
!=
NULL
;
el
=
el
->
next
)
{
pr_mem_size
+=
strlen
((
char
*
)
el
->
param
)
+
1
;
}
if
(
str
==
NULL
)
goto
out
;
// allocating protected memory region for parameter
param_size
=
1
+
strlen
(
str
);
res
=
(
char
*
)
mmap
(
NULL
,
param_size
,
PROT_READ
|
PROT_WRITE
,
MAP_PRIVATE
|
MAP_ANON
,
-
1
,
0
);
if
(
!
res
)
{
log_err
(
LD_BUG
,
"(Sandbox) failed allocate protected memory!"
);
// allocate protected memory
pr_mem_base
=
(
char
*
)
mmap
(
NULL
,
pr_mem_size
,
PROT_READ
|
PROT_WRITE
,
MAP_PRIVATE
|
MAP_ANON
,
-
1
,
0
);
if
(
pr_mem_base
==
MAP_FAILED
)
{
log_err
(
LD_BUG
,
"(Sandbox) failed allocate protected memory! mmap: %s"
,
strerror
(
errno
));
ret
=
-
1
;
goto
out
;
}
// copying from non protected to protected + pointer reassign
memcpy
(
res
,
str
,
param_size
);
pr_mem_next
=
pr_mem_base
;
pr_mem_left
=
pr_mem_size
;
// change el value pointer to protected
for
(
el
=
cfg
;
el
!=
NULL
;
el
=
el
->
next
)
{
char
*
param_val
=
(
char
*
)
el
->
param
;
int
param_size
=
strlen
(
param_val
)
+
1
;
if
(
pr_mem_left
-
param_size
>=
0
)
{
// copy to protected
memcpy
(
pr_mem_next
,
param_val
,
param_size
);
// re-point el parameter to protected
free
((
char
*
)
el
->
param
);
el
->
param
=
(
intptr_t
)
pr_mem_next
;
el
->
prot
=
1
;
// move next available protected memory
pr_mem_next
+=
param_size
;
pr_mem_left
-=
param_size
;
}
else
{
log_err
(
LD_BUG
,
"(Sandbox) insufficient protected memory!"
);
ret
=
-
2
;
goto
out
;
}
}
// protecting from writes
if
(
mprotect
(
res
,
param_size
,
PROT_READ
))
{
log_err
(
LD_BUG
,
"(Sandbox) failed to protect memory!"
);
return
NULL
;
if
(
mprotect
(
pr_mem_base
,
pr_mem_size
,
PROT_READ
))
{
log_err
(
LD_BUG
,
"(Sandbox) failed to protect memory! mprotect: %s"
,
strerror
(
errno
));
ret
=
-
3
;
goto
out
;
}
out:
return
re
s
;
return
re
t
;
}
#ifdef __NR_stat64
...
...
@@ -719,8 +748,8 @@ sandbox_cfg_allow_stat64_filename(sandbox_cfg_t **cfg, char *file, int fr)
elem
=
(
sandbox_cfg_t
*
)
malloc
(
sizeof
(
sandbox_cfg_t
));
elem
->
syscall
=
SCMP_SYS
(
stat64
);
elem
->
pindex
=
0
;
elem
->
param
=
(
intptr_t
)
prot_
strdup
(
(
char
*
)
file
);
elem
->
prot
=
1
;
elem
->
param
=
(
intptr_t
)
strdup
(
file
);
elem
->
prot
=
0
;
elem
->
next
=
*
cfg
;
*
cfg
=
elem
;
...
...
@@ -763,8 +792,8 @@ sandbox_cfg_allow_open_filename(sandbox_cfg_t **cfg, char *file, int fr)
elem
=
(
sandbox_cfg_t
*
)
malloc
(
sizeof
(
sandbox_cfg_t
));
elem
->
syscall
=
SCMP_SYS
(
open
);
elem
->
pindex
=
0
;
elem
->
param
=
(
intptr_t
)
prot_
strdup
(
(
char
*
)
file
);
elem
->
prot
=
1
;
elem
->
param
=
(
intptr_t
)
strdup
(
file
);
elem
->
prot
=
0
;
elem
->
next
=
*
cfg
;
*
cfg
=
elem
;
...
...
@@ -806,8 +835,8 @@ sandbox_cfg_allow_openat_filename(sandbox_cfg_t **cfg, char *file, int fr)
elem
=
(
sandbox_cfg_t
*
)
malloc
(
sizeof
(
sandbox_cfg_t
));
elem
->
syscall
=
SCMP_SYS
(
openat
);
elem
->
pindex
=
1
;
elem
->
param
=
(
intptr_t
)
prot_
strdup
(
(
char
*
)
file
);
;
elem
->
prot
=
1
;
elem
->
param
=
(
intptr_t
)
strdup
(
file
);
elem
->
prot
=
0
;
elem
->
next
=
*
cfg
;
*
cfg
=
elem
;
...
...
@@ -849,8 +878,8 @@ sandbox_cfg_allow_execve(sandbox_cfg_t **cfg, char *com)
elem
=
(
sandbox_cfg_t
*
)
malloc
(
sizeof
(
sandbox_cfg_t
));
elem
->
syscall
=
SCMP_SYS
(
openat
);
elem
->
pindex
=
1
;
elem
->
param
=
(
intptr_t
)
prot_
strdup
(
(
char
*
)
com
);
;
elem
->
prot
=
1
;
elem
->
param
=
(
intptr_t
)
strdup
(
com
);
elem
->
prot
=
0
;
elem
->
next
=
*
cfg
;
*
cfg
=
elem
;
...
...
@@ -1121,7 +1150,7 @@ initialise_libseccomp_sandbox(sandbox_cfg_t* cfg)
if
(
install_sigsys_debugging
())
return
-
1
;
if
(
init_addrinfo
())
{
if
(
init_addrinfo
()
||
prot_strings
(
cfg
)
)
{
return
-
4
;
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment