Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
D
Dnsmasq
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Packages
Packages
List
Container Registry
Analytics
Analytics
CI / CD
Code Review
Insights
Issues
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
nanahira
Dnsmasq
Commits
79cfefd8
Commit
79cfefd8
authored
Sep 02, 2012
by
Simon Kelley
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Make pid-file creation immune to symlink attack.
parent
0c0d4793
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
39 additions
and
6 deletions
+39
-6
src/dnsmasq.c
src/dnsmasq.c
+39
-6
No files found.
src/dnsmasq.c
View file @
79cfefd8
...
...
@@ -383,15 +383,48 @@ int main (int argc, char **argv)
/* write pidfile _after_ forking ! */
if
(
daemon
->
runfile
)
{
FILE
*
pidfile
;
int
fd
,
err
=
0
;
sprintf
(
daemon
->
namebuff
,
"%d
\n
"
,
(
int
)
getpid
());
/* Explanation: Some installations of dnsmasq (eg Debian/Ubuntu) locate the pid-file
in a directory which is writable by the non-privileged user that dnsmasq runs as. This
allows the daemon to delete the file as part of its shutdown. This is a security hole to the
extent that an attacker running as the unprivileged user could replace the pidfile with a
symlink, and have the target of that symlink overwritten as root next time dnsmasq starts.
The folowing code first deletes any existing file, and then opens it with the O_EXCL flag,
ensuring that the open() fails should there be any existing file (because the unlink() failed,
or an attacker exploited the race between unlink() and open()). This ensures that no symlink
attack can succeed.
Any compromise of the non-privileged user still theoretically allows the pid-file to be
replaced whilst dnsmasq is running. The worst that could allow is that the usual
"shutdown dnsmasq" shell command could be tricked into stopping any other process.
Note that if dnsmasq is started as non-root (eg for testing) it silently ignores
failure to write the pid-file.
*/
unlink
(
daemon
->
runfile
);
/* only complain if started as root */
if
((
pidfile
=
fopen
(
daemon
->
runfile
,
"w"
)))
if
((
fd
=
open
(
daemon
->
runfile
,
O_WRONLY
|
O_CREAT
|
O_TRUNC
|
O_EXCL
,
S_IWUSR
|
S_IRUSR
|
S_IRGRP
|
S_IROTH
))
==
-
1
)
{
fprintf
(
pidfile
,
"%d
\n
"
,
(
int
)
getpid
());
fclose
(
pidfile
);
/* only complain if started as root */
if
(
getuid
()
==
0
)
err
=
1
;
}
else
if
(
getuid
()
==
0
)
else
{
if
(
!
read_write
(
fd
,
(
unsigned
char
*
)
daemon
->
namebuff
,
strlen
(
daemon
->
namebuff
),
0
))
err
=
1
;
while
(
!
err
&&
close
(
fd
)
==
-
1
)
if
(
!
retry_send
())
err
=
1
;
}
if
(
err
)
{
send_event
(
err_pipe
[
1
],
EVENT_PIDFILE
,
errno
,
daemon
->
runfile
);
_exit
(
0
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment