summary refs log tree commit diff stats
path: root/lib/pure/strutils.nim
diff options
context:
space:
mode:
authoralaviss <alaviss@users.noreply.github.com>2020-04-20 15:09:59 +0000
committerGitHub <noreply@github.com>2020-04-20 17:09:59 +0200
commit1bdc30bdb13422bd5eff830327582a938e7b76ac (patch)
tree48c3d5e4f677042b3dd750cae5a255768a03da1a /lib/pure/strutils.nim
parent6bd279c97871e47c78636f3c1a8f39a7915b1402 (diff)
downloadNim-1bdc30bdb13422bd5eff830327582a938e7b76ac.tar.gz
Make file descriptors from stdlib non-inheritable by default (#13201)
* io: make file descriptors non-inheritable by default

This prevents file descriptors/handles leakage to child processes
that might cause issues like running out of file descriptors, or potential
security issues like leaking a file descriptor to a restricted file.

While this breaks backward compatibility, I'm rather certain that not
many programs (if any) actually make use of this implementation detail.
A new API `setInheritable` is provided for the few that actually want to
use this functionality.

* io: disable inheritance at file creation time for supported platforms

Some platforms provide extension to fopen-family of functions to allow
for disabling descriptor inheritance atomically during File creation.
This guards against possible leaks when a child process is spawned
before we managed to disable the file descriptor inheritance
(ie. in a multi-threaded program).

* net, nativesockets: make sockets non inheritable by default

With this commit, sockets will no longer leak to child processes when
you don't want it to. Should solves a lot of "address in use" that might
occur when your server has just restarted.

All APIs that create sockets in these modules now expose a `inheritable`
flag that allow users to toggle inheritance for the resulting sockets.
An implementation of `setInheritance()` is also provided for SocketHandle.

While atomically disabling inheritance at creation time is supported on
Windows, it's only implemented by native winsock2, which is too much for
now. This support can be implemented in a future patch.

* posix: add F_DUPFD_CLOEXEC

This command duplicates file descriptor with close-on-exec flag set.

Defined in POSIX.1-2008.

* ioselectors_kqueue: don't leak file descriptors

File descriptors internally used by ioselectors on BSD/OSX are now
shielded from leakage.

* posix: add O_CLOEXEC

This flag allows file descriptors to be open() with close-on-exec flag
set atomically.

This flag is specified in POSIX.1-2008

* tfdleak: test for selectors leakage

Also simplified the test by using handle-type agnostic APIs to test for
validity.

* ioselectors_epoll: mark all fd created close-on-exec

File descriptors from ioselectors should no longer leaks on Linux.

* tfdleak: don't check for selector leakage on Windows

The getFd proc for ioselectors_select returns a hardcoded -1

* io: add NoInheritFlag at compile time

* io: add support for ioctl-based close-on-exec

This allows for the flag to be set/unset in one syscall. While the
performance gains might be negliable, we have one less failure point
to deal with.

* tfdleak: add a test for setInheritable

* stdlib: add nimInheritHandles to restore old behaviors

* memfiles: make file handle not inheritable by default for posix

* io: setInheritable now operates on OS file handle

On Windows, the native handle is the only thing that's inheritable, thus
we can assume that users of this function will already have the handle
available to them. This also allows users to pass down file descriptors
from memfiles on Windows with ease, should that be desired.

With this, nativesockets.setInheritable can be made much simpler.

* changelog: clarify

* nativesockets: document setInheritable return value

* posix_utils: atomically disable fd inheritance for mkstemp
Diffstat (limited to 'lib/pure/strutils.nim')
0 files changed, 0 insertions, 0 deletions
evision' href='/ahoang/Nim/blame/compiler/layouter.nim?h=devel&id=bf5d619a52da04c857a6f7fb3d68afc12182bc22'>^
dd81d9d5b ^





a1bd4a6cb ^





dd81d9d5b ^

a1bd4a6cb ^

837d0c727 ^




a1bd4a6cb ^


















dba26656f ^
1be82d96a ^



dd81d9d5b ^

a1bd4a6cb ^




dba26656f ^
dd81d9d5b ^
7819e63f7 ^
a1bd4a6cb ^





c3090fcb4 ^

a1bd4a6cb ^

7819e63f7 ^
a1bd4a6cb ^



c3090fcb4 ^




a1bd4a6cb ^






98f3daea6 ^
a1bd4a6cb ^




















dd81d9d5b ^










dba26656f ^







dd81d9d5b ^





dba26656f ^
a1bd4a6cb ^




97398edc0 ^

1be82d96a ^


97398edc0 ^

dd81d9d5b ^

a1bd4a6cb ^
dd81d9d5b ^









a1bd4a6cb ^





a1bd4a6cb ^
c3090fcb4 ^
7819e63f7 ^











a1bd4a6cb ^
a1bd4a6cb ^
0725003a8 ^
a1bd4a6cb ^
0725003a8 ^
a1bd4a6cb ^
98f3daea6 ^








c3090fcb4 ^
98f3daea6 ^

a1bd4a6cb ^
0725003a8 ^
a1bd4a6cb ^



























7819e63f7 ^

5976bd96b ^


7819e63f7 ^






1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274