/mandos/trunk

To get this branch, use:
bzr branch http://bzr.recompile.se/loggerhead/mandos/trunk

« back to all changes in this revision

Viewing changes to initramfs-tools-hook

  • Committer: teddy at recompile
  • Date: 2020-04-05 21:30:59 UTC
  • Revision ID: teddy@recompile.se-20200405213059-fb2a61ckqynrmatk
Fix file descriptor leak in mandos-client

When the local network has Mandos servers announcing themselves using
real, globally reachable, IPv6 addresses (i.e. not link-local
addresses), but there is no router on the local network providing IPv6
RA (Router Advertisement) packets, the client cannot reach the server
by normal means, since the client only has a link-local IPv6 address,
and has no usable route to reach the server's global IPv6 address.
(This is not a common situation, and usually only happens when the
router itself reboots and runs a Mandos client, since it cannot then
give RA packets to itself.)  The client code has a solution for
this, which consists of adding a temporary local route to reach the
address of the server during communication, and removing this
temporary route afterwards.

This solution with a temporary route works, but has a file descriptor
leak; it leaks one file descriptor for each addition and for each
removal of a route.  If one server requiring an added route is present
on the network, but no servers gives a password, making the client
retry after the default ten seconds, and we furthermore assume a
default 1024 open files limit, the client runs out of file descriptors
after about 90 minutes, after which time the client process will be
useless and fail to retrieve any passwords, necessitating manual
password entry via the keyboard.

Fix this by eliminating the file descriptor leak in the client.

* plugins.d/mandos-client.c (add_delete_local_route): Do
  close(devnull) also in parent process, also if fork() fails, and on
  any failure in child process.

Show diffs side-by-side

added added

removed removed

Lines of Context:
3
3
# This script will be run by 'mkinitramfs' when it creates the image.
4
4
# Its job is to decide which files to install, then install them into
5
5
# the staging area, where the initramfs is being created.  This
6
 
# happens when a new 'linux-image' package is installed, or when the
 
6
# happens when a new 'linux-image' package is installed, or when an
7
7
# administrator runs 'update-initramfs' by hand to update an initramfs
8
8
# image.
9
9
 
29
29
 
30
30
. /usr/share/initramfs-tools/hook-functions
31
31
 
32
 
for d in /usr /usr/local; do
33
 
    if [ -d "$d"/lib/mandos ]; then
34
 
        prefix="$d"
 
32
for d in /usr/lib \
 
33
    "/usr/lib/`dpkg-architecture -qDEB_HOST_MULTIARCH 2>/dev/null`" \
 
34
    "`rpm --eval='%{_libdir}' 2>/dev/null`" /usr/local/lib; do
 
35
    if [ -d "$d"/mandos ]; then
 
36
        libdir="$d"
35
37
        break
36
38
    fi
37
39
done
38
 
if [ -z "$prefix" ]; then
 
40
if [ -z "$libdir" ]; then
39
41
    # Mandos not found
40
42
    exit 1
41
43
fi
51
53
    exit 1
52
54
fi
53
55
 
54
 
mandos_user="`{ getent passwd mandos \
55
 
                || getent passwd nobody \
56
 
                || echo ::65534::::; } \
57
 
        | awk --field-separator=: '{ print $3 }'`" 
58
 
mandos_group="`{ getent group mandos \
59
 
                || getent group nogroup \
60
 
                || echo ::65534:; } \
61
 
        | awk --field-separator=: '{ print $3 }'`"
 
56
set `{ getent passwd _mandos \
 
57
    || getent passwd nobody \
 
58
    || echo ::65534:65534:::; } \
 
59
    | cut --delimiter=: --fields=3,4 --only-delimited \
 
60
    --output-delimiter=" "`
 
61
mandos_user="$1"
 
62
mandos_group="$2"
62
63
 
63
64
# The Mandos network client uses the network
64
65
auto_add_modules net
69
70
CONFDIR="/conf/conf.d/mandos"
70
71
MANDOSDIR="/lib/mandos"
71
72
PLUGINDIR="${MANDOSDIR}/plugins.d"
 
73
PLUGINHELPERDIR="${MANDOSDIR}/plugin-helpers"
 
74
HOOKDIR="${MANDOSDIR}/network-hooks.d"
72
75
 
73
76
# Make directories
74
77
install --directory --mode=u=rwx,go=rx "${DESTDIR}${CONFDIR}" \
75
 
        "${DESTDIR}${MANDOSDIR}"
 
78
        "${DESTDIR}${MANDOSDIR}" "${DESTDIR}${HOOKDIR}"
76
79
install --owner=${mandos_user} --group=${mandos_group} --directory \
77
 
    --mode=u=rwx "${DESTDIR}${PLUGINDIR}"
 
80
        --mode=u=rwx "${DESTDIR}${PLUGINDIR}" \
 
81
        "${DESTDIR}${PLUGINHELPERDIR}"
 
82
 
 
83
copy_exec "$libdir"/mandos/mandos-to-cryptroot-unlock "${MANDOSDIR}"
78
84
 
79
85
# Copy the Mandos plugin runner
80
 
copy_exec "$prefix"/lib/mandos/plugin-runner "${MANDOSDIR}"
 
86
copy_exec "$libdir"/mandos/plugin-runner "${MANDOSDIR}"
81
87
 
82
88
# Copy the plugins
83
89
 
84
90
# Copy the packaged plugins
85
 
for file in "$prefix"/lib/mandos/plugins.d/*; do
 
91
for file in "$libdir"/mandos/plugins.d/*; do
86
92
    base="`basename \"$file\"`"
87
93
    # Is this plugin overridden?
88
94
    if [ -e "/etc/mandos/plugins.d/$base" ]; then
89
95
        continue
90
96
    fi
91
97
    case "$base" in
92
 
        *~|.*|\#*\#|*.dpkg-old|*.dpkg-new|*.dpkg-divert) : ;;
93
 
        "*") :;;
94
 
        *) copy_exec "$file" "${PLUGINDIR}";;
 
98
        *~|.*|\#*\#|*.dpkg-old|*.dpkg-bak|*.dpkg-new|*.dpkg-divert)
 
99
            : ;;
 
100
        "*") echo "W: Mandos client plugin directory is empty." >&2 ;;
 
101
        *) copy_exec "$file" "${PLUGINDIR}" ;;
 
102
    esac
 
103
done
 
104
 
 
105
# Copy the packaged plugin helpers
 
106
for file in "$libdir"/mandos/plugin-helpers/*; do
 
107
    base="`basename \"$file\"`"
 
108
    # Is this plugin overridden?
 
109
    if [ -e "/etc/mandos/plugin-helpers/$base" ]; then
 
110
        continue
 
111
    fi
 
112
    case "$base" in
 
113
        *~|.*|\#*\#|*.dpkg-old|*.dpkg-bak|*.dpkg-new|*.dpkg-divert)
 
114
            : ;;
 
115
        "*") : ;;
 
116
        *) copy_exec "$file" "${PLUGINHELPERDIR}" ;;
95
117
    esac
96
118
done
97
119
 
99
121
for file in /etc/mandos/plugins.d/*; do
100
122
    base="`basename \"$file\"`"
101
123
    case "$base" in
102
 
        *~|.*|\#*\#|*.dpkg-old|*.dpkg-new|*.dpkg-divert) : ;;
103
 
        "*") :;;
104
 
        *) copy_exec "$file" "${PLUGINDIR}";;
105
 
    esac
106
 
done
107
 
 
108
 
# GPGME needs /usr/bin/gpg
109
 
if [ ! -e "${DESTDIR}/usr/bin/gpg" \
110
 
    -a -n "`ls \"${DESTDIR}\"/usr/lib/libgpgme.so* \
111
 
                2>/dev/null`" ]; then
112
 
    copy_exec /usr/bin/gpg
113
 
fi
 
124
        *~|.*|\#*\#|*.dpkg-old|*.dpkg-bak|*.dpkg-new|*.dpkg-divert)
 
125
            : ;;
 
126
        "*") : ;;
 
127
        *) copy_exec "$file" "${PLUGINDIR}" ;;
 
128
    esac
 
129
done
 
130
 
 
131
# Copy any user-supplied plugin helpers
 
132
for file in /etc/mandos/plugin-helpers/*; do
 
133
    base="`basename \"$file\"`"
 
134
    case "$base" in
 
135
        *~|.*|\#*\#|*.dpkg-old|*.dpkg-bak|*.dpkg-new|*.dpkg-divert)
 
136
            : ;;
 
137
        "*") : ;;
 
138
        *) copy_exec "$file" "${PLUGINHELPERDIR}" ;;
 
139
    esac
 
140
done
 
141
 
 
142
# Get DEVICE from initramfs.conf and other files
 
143
. /etc/initramfs-tools/initramfs.conf
 
144
for conf in /etc/initramfs-tools/conf.d/*; do
 
145
    if [ -n "`basename \"$conf\" \
 
146
        | grep '^[[:alnum:]][[:alnum:]\._-]*$' \
 
147
        | grep -v '\.dpkg-.*$'`" ]; then
 
148
        [ -f "${conf}" ] && . "${conf}"
 
149
    fi
 
150
done
 
151
export DEVICE
 
152
 
 
153
# Copy network hooks
 
154
for hook in /etc/mandos/network-hooks.d/*; do
 
155
    case "`basename \"$hook\"`" in
 
156
        "*") continue ;;
 
157
        *[!A-Za-z0-9_.-]*) continue ;;
 
158
        *) test -d "$hook" || copy_exec "$hook" "${HOOKDIR}" ;;
 
159
    esac
 
160
    if [ -x "$hook" ]; then
 
161
        # Copy any files needed by the network hook
 
162
        MANDOSNETHOOKDIR=/etc/mandos/network-hooks.d MODE=files \
 
163
            VERBOSITY=0 "$hook" files | while read -r file target; do
 
164
            if [ ! -e "${file}" ]; then
 
165
                echo "WARNING: file ${file} not found, requested by Mandos network hook '${hook##*/}'" >&2
 
166
            fi
 
167
            if [ -z "${target}" ]; then
 
168
                copy_exec "$file"
 
169
            else
 
170
                copy_exec "$file" "$target"
 
171
            fi
 
172
        done
 
173
        # Copy and load any modules needed by the network hook
 
174
        MANDOSNETHOOKDIR=/etc/mandos/network-hooks.d MODE=modules \
 
175
            VERBOSITY=0 "$hook" modules | while read -r module; do
 
176
            force_load "$module"
 
177
        done
 
178
    fi
 
179
done
 
180
 
 
181
# GPGME needs GnuPG
 
182
gpg=/usr/bin/gpg
 
183
libgpgme11_version="`dpkg-query --showformat='${Version}' --show libgpgme11`"
 
184
if dpkg --compare-versions "$libgpgme11_version" ge 1.5.0-0.1; then
 
185
    if [ -e /usr/bin/gpgconf ]; then
 
186
        if [ ! -e "${DESTDIR}/usr/bin/gpgconf" ]; then
 
187
            copy_exec /usr/bin/gpgconf
 
188
        fi
 
189
        gpg="`/usr/bin/gpgconf|sed --quiet --expression='s/^gpg:[^:]*://p'`"
 
190
        gpgagent="`/usr/bin/gpgconf|sed --quiet --expression='s/^gpg-agent:[^:]*://p'`"
 
191
        # Newer versions of GnuPG 2 requires the gpg-agent binary
 
192
        if [ -e "$gpgagent" ] && [ ! -e "${DESTDIR}$gpgagent" ]; then
 
193
            copy_exec "$gpgagent"
 
194
        fi
 
195
    fi
 
196
elif dpkg --compare-versions "$libgpgme11_version" ge 1.4.1-0.1; then
 
197
    gpg=/usr/bin/gpg2
 
198
fi
 
199
if [ ! -e "${DESTDIR}$gpg" ]; then
 
200
    copy_exec "$gpg"
 
201
fi
 
202
unset gpg
 
203
unset libgpgme11_version
114
204
 
115
205
# Config files
116
 
for file in /etc/mandos/*; do
117
 
    if [ -d "$file" ]; then
118
 
        continue
119
 
    fi
120
 
    cp --archive --sparse=always "$file" "${DESTDIR}${CONFDIR}"
121
 
done
122
 
 
123
 
# Key files 
124
 
for file in  "$keydir"/*; do
125
 
    if [ -d "$file" ]; then
126
 
        continue
127
 
    fi
128
 
    cp --archive --sparse=always "$file" "${DESTDIR}${CONFDIR}"
129
 
    chown ${mandos_user}:${mandos_group} \
130
 
        "${DESTDIR}${CONFDIR}/`basename \"$file\"`"
131
 
done
 
206
for file in /etc/mandos/plugin-runner.conf; do
 
207
    if [ -d "$file" ]; then
 
208
        continue
 
209
    fi
 
210
    cp --archive --sparse=always "$file" "${DESTDIR}${CONFDIR}"
 
211
done
 
212
 
 
213
if [ ${mandos_user} != 65534 ]; then
 
214
    sed --in-place --expression="1i--userid=${mandos_user}" \
 
215
        "${DESTDIR}${CONFDIR}/plugin-runner.conf"
 
216
fi
 
217
 
 
218
if [ ${mandos_group} != 65534 ]; then
 
219
    sed --in-place --expression="1i--groupid=${mandos_group}" \
 
220
        "${DESTDIR}${CONFDIR}/plugin-runner.conf"
 
221
fi
 
222
 
 
223
# Key files
 
224
for file in "$keydir"/*; do
 
225
    if [ -d "$file" ]; then
 
226
        continue
 
227
    fi
 
228
    case "$file" in
 
229
        *~|.*|\#*\#|*.dpkg-old|*.dpkg-bak|*.dpkg-new|*.dpkg-divert)
 
230
            : ;;
 
231
        "*") : ;;
 
232
        *)
 
233
            cp --archive --sparse=always "$file" \
 
234
               "${DESTDIR}${CONFDIR}"
 
235
            chown ${mandos_user}:${mandos_group} \
 
236
                  "${DESTDIR}${CONFDIR}/`basename \"$file\"`"
 
237
            ;;
 
238
    esac
 
239
done
 
240
# Use Diffie-Hellman parameters file if available
 
241
if [ -e "${DESTDIR}${CONFDIR}"/dhparams.pem ]; then
 
242
    sed --in-place \
 
243
        --expression="1i--options-for=mandos-client:--dh-params=${CONFDIR}/dhparams.pem" \
 
244
        "${DESTDIR}/${CONFDIR}/plugin-runner.conf"
 
245
fi
132
246
 
133
247
# /lib/mandos/plugin-runner will drop priviliges, but needs access to
134
248
# its plugin directory and its config file.  However, since almost all
139
253
# initrd; it is intended to affect the initrd.img file itself, since
140
254
# it now contains secret key files.  There is, however, no other way
141
255
# to set the permission of the initrd.img file without a race
142
 
# condition.  This umask is set by "initramfs-tools-hook-conf",
143
 
# installed as "/usr/share/initramfs-tools/conf-hooks.d/mandos".)
 
256
# condition.  This umask is set by "initramfs-tools-conf", installed
 
257
# as "/usr/share/initramfs-tools/conf.d/mandos-conf".)
144
258
145
259
for full in "${MANDOSDIR}" "${CONFDIR}"; do
146
260
    while [ "$full" != "/" ]; do
156
270
        chmod a+rX "${DESTDIR}$dir"
157
271
    fi
158
272
done
159
 
for dir in /lib /usr/lib; do
160
 
    find "${DESTDIR}$dir" \! -perm -u+rw,g+r -prune -or -print0 \
161
 
        | xargs --null --no-run-if-empty chmod a+rX
 
273
for dir in "${DESTDIR}"/lib* "${DESTDIR}"/usr/lib*; do
 
274
    if [ -d "$dir" ]; then
 
275
        find "$dir" \! -perm -u+rw,g+r -prune -or \! -type l -print0 \
 
276
            | xargs --null --no-run-if-empty chmod a+rX --
 
277
    fi
162
278
done