Repository navigation
Expand file tree
/
Copy pathindex.xml
More file actions
497 lines (483 loc) · 19.7 KB
/
Copy pathindex.xml
File metadata and controls
497 lines (483 loc) · 19.7 KB
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
<!DOCTYPE html>
<html lang="en" prefix="og: http://ogp.me/ns#">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta charset="utf-8" />
<title>kcgi | minimal CGI and FastCGI library for C/C++</title>
<link rel="alternate" href="atom.xml" type="application/atom+xml" title="kcgi version feed" />
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css" />
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Alegreya+Sans:400,400italic,500,700" />
<link rel="stylesheet" href="https://bsd.lv/css/style.css" />
<link rel="stylesheet" href="index.css" />
<meta property="og:title" content="kcgi: minimal CGI and FastCGI library for C/C++" />
<meta property="og:description"
content="A C/C++ library for security-oriented web applications." />
<meta property="og:url" content="https://kristaps.bsd.lv/kcgi/index.html" />
<meta property="og:type" content="website" />
<meta name="description"
content="A C/C++ library for security-oriented web applications." />
</head>
<body>
<header>
<section id="breadcrumbs" class="text">
<a href="https://www.bsd.lv">BSD.lv</a> tools for <a href="https://learnbchs.org">BCHS</a>:
<a href="https://kristaps.bsd.lv/kcgi">kcgi</a>,
<a href="https://kristaps.bsd.lv/sqlbox">sqlbox</a>,
<a href="https://kristaps.bsd.lv/openradtool">ort</a>
</section>
<section id="header" class="text">
<h1>
<a href="index.html" class="nm">kcgi</a>
–
<span class="nd">
minimal CGI and FastCGI library for C/C++
</span>
</h1>
<nav>
<span class="version">
Version
<nav data-sblg-nav="1" data-sblg-navtag="version"
data-sblg-navsz="1" data-sblg-navxml="1">
${sblg-titletext}
</nav>
</span>
<a title="Source code" href="snapshots/kcgi.tgz">
<i class="fa fa-fw fa-gears"></i>
</a>
<a title="Source fingerprint" href="snapshots/kcgi.tgz.sha512">
<i class="fa fa-fw fa-lock"></i>
</a>
<a title="Source archive" href="snapshots">
<i class="fa fa-fw fa-archive"></i>
</a>
<a title="Source repository" href="https://www.github.com/kristapsdz/kcgi">
<i class="fa fa-fw fa-github"></i>
</a>
<a title="Version feed" href="atom.xml">
<i class="fa fa-fw fa-rss"></i>
</a>
</nav>
</section>
</header>
<section class="text">
<p>
<span class="nm">kcgi</span> is an
<a href="http://opensource.org/licenses/ISC" rel="license">open source</a>
CGI and FastCGI library for C/C++ web applications. It is minimal,
secure, and auditable. To start, <a href="#install">install</a> the
library. Then read the <a href="#deploying">deployment</a> and
<a href="#usage">usage</a> guides.
</p>
</section>
<figure id="sample">
<input type="radio" name="sample-show" value="0" id="show-nocomments" checked="checked" />
<input type="radio" name="sample-show" value="1" id="show-comments" />
<nav class="text">
<label id="label-nocomments" for="show-nocomments">
<i class="fa fa-fw fa-comments-o"></i> Hide source comments
</label>
<label id="label-comments" for="show-comments">
<i class="fa fa-fw fa-comments"></i> Show source comments
</label>
</nav>
<figcaption class="text">
The following echoes <q>Hello, World!</q> as an HTTP response to a
CGI request.
</figcaption>
<pre id="nocomments" class="text prettyprint"><span class="cpp">#include</span> <span class="literal"><sys/types.h></span> /* size_t, ssize_t */
<span class="cpp">#include</span> <span class="literal"><stdarg.h></span> /* va_list */
<span class="cpp">#include</span> <span class="literal"><stddef.h></span> /* NULL */
<span class="cpp">#include</span> <span class="literal"><stdint.h></span> /* int64_t */
<span class="cpp">#include</span> <span class="literal"><<a href="kcgi.3.html">kcgi.h</a>></span>
<span class="ident">int</span> main(<span class="ident">void</span>) {
<span class="ident">struct</span> kreq r;
<span class="ident">const char</span> *page = <span class="literal">"index"</span>;
<span class="flow">if</span> (<a href="khttp_parse.3.html">khttp_parse</a>(&r, NULL, 0, &page, 1, 0) != KCGI_OK)
return 1;
<a href="khttp_head.3.html">khttp_head</a>(&r, kresps[KRESP_STATUS],
<span class="literal">"%s"</span>, khttps[KHTTP_200]);
khttp_head(&r, kresps[KRESP_CONTENT_TYPE],
<span class="literal">"%s"</span>, kmimetypes[KMIME_TEXT_PLAIN]);
<a href="khttp_body.3.html">khttp_body</a>(&r);
<a href="khttp_write.3.html">khttp_puts</a>(&r, <span class="literal">"Hello, world!"</span>);
<a href="khttp_free.3.html">khttp_free</a>(&r);
<span class="flow">return</span> 0;
}</pre>
<pre id="comments" class="text prettyprint"><span class="cpp">#include</span> <span class="literal"><sys/types.h></span> /* size_t, ssize_t */
<span class="cpp">#include</span> <span class="literal"><stdarg.h></span> /* va_list */
<span class="cpp">#include</span> <span class="literal"><stddef.h></span> /* NULL */
<span class="cpp">#include</span> <span class="literal"><stdint.h></span> /* int64_t */
<span class="cpp">#include</span> <span class="literal"><<a href="kcgi.3.html">kcgi.h</a>></span>
<span class="ident">int</span> main(<span class="ident">void</span>) {
<span class="ident">struct</span> kreq r;
<span class="ident">const char</span> *page = <span class="literal">"index"</span>;
/*
* Parse the HTTP environment.
* We only know a single page, "index", which is also
* the default page if none is supplied.
* (We don't validate any input fields.)
*/
<span class="flow">if</span> (<a href="khttp_parse.3.html">khttp_parse</a>(&r, NULL, 0, &page, 1, 0) != KCGI_OK)
return 1;
/*
* Ordinarily, here I'd switch on the method (OPTIONS, etc.,
* defined in the <q>method</q> variable) then switch on which
* page was requested (<q>page</q> variable).
* But for brevity's sake, just output a response: HTTP 200.
*/
<a href="khttp_head.3.html">khttp_head</a>(&r, kresps[KRESP_STATUS],
<span class="literal">"%s"</span>, khttps[KHTTP_200]);
/*
* Show content-type unilaterally as text/plain.
* This would usually be set from r.mime.
*/
khttp_head(&r, kresps[KRESP_CONTENT_TYPE],
<span class="literal">"%s"</span>, kmimetypes[KMIME_TEXT_PLAIN]);
/* No more HTTP headers: start the HTTP document body. */
<a href="khttp_body.3.html">khttp_body</a>(&r);
/*
* We can put any content below here: JSON, HTML, etc.
* Usually we'd switch on our MIME type.
* However, we're just going to put the literal string as noted…
*/
<a href="khttp_write.3.html">khttp_puts</a>(&r, <span class="literal">"Hello, world!"</span>);
/* Flush the document and free resources. */
<a href="khttp_free.3.html">khttp_free</a>(&r);
<span class="flow">return</span> 0;
}</pre>
</figure>
<section class="text">
<p>
For fuller examples, see <a href="sample.c.html">sample.c</a> (C and
CGI), <a href="samplepp.cc.html">samplepp.cc</a> (C++ and CGI),
<a href="sample-fcgi.c.html">sample-fcgi.c</a> (C and FastCGI), or
jump to the <a href="#usage">Documentation</a> section.
</p>
<p>
<i>kcgi</i> supports many features: auto-compression, handling of
all HTTP input operations (query strings, cookies, page bodies,
multipart) with validation, authentication, configurable output
caching, request debugging, formatted writers (JSON, XML, etc.) and
so on. Its strongest differentiating feature is using sandboxing
and process separation for handling the untrusted input path.
</p>
</section>
<section id="version">
<div class="text">
<h2>
current release
</h2>
<nav data-sblg-nav="1" data-sblg-navtag="version" data-sblg-navsz="1"
data-sblg-navcontent="1">
<div>
<div class="title">
Version <span class="version">${sblg-title}</span>:
<time datetime="${sblg-date}">${sblg-date}</time>
</div>
<div class="content">${sblg-aside}</div>
</div>
</nav>
<p class="archive">
(<a href="archive.html">release archive</a>)
</p>
</div>
</section>
<section id="install" class="text">
<h2>
installation
</h2>
<p>
First, check if <span class="nm">kcgi</span> isn't already packaged
for your system, such as with
<code>pkg_add kcgi</code> (OpenBSD),
<code>pkg install kcgi</code> (FreeBSD),
<code>brew install kcgi</code> (brew),
<code>pacman -S kcgi</code> (Arch Linux),
and so on. If so, install using that system.
</p>
<p>
If not, <span class="nm">kcgi</span> has been built and run on
GNU/<a href="https://www.linux.org">Linux</a> (musl and glibc),
<a href="https://www.openbsd.org">OpenBSD</a>,
<a href="https://www.netbsd.org">NetBSD</a>,
<a href="https://www.freebsd.org">FreeBSD</a>),
<a href="https://www.oracle.com/solaris/solaris11">Solaris</a>,
<a href="https://omniosce.org">OmniOS</a>, and
<a href="https://www.apple.com/osx">Mac OS X</a>
(only Mojave and newer!) on i386, amd64, powerpc, arm64, and
sparc64.
Download <a href="snapshots/kcgi.tgz">kcgi.tgz</a> and verify the
archive with
<a href="snapshots/kcgi.tgz.sha512">kcgi.tgz.sha512</a>.
To run bleeding-edge code between releases, the
repository is on
<a href="https://github.com/kristapsdz/kcgi">GitHub</a>.
In both cases, build and install instructions may be
found on the repository page.
</p>
<p>
The only hard build-time dependency is BSD make
(<code>bmake</code> on Linux)
and <a href="https://www.zlib.net/">zlib</a>.
If you're running the regression tests (see
<a href="#testing">Testing</a>), you'll also need
<a href="https://curl.haxx.se/libcurl/">libcurl</a>.
</p>
</section>
<section id="deploying" class="text">
<h2>
deployment
</h2>
<p>
To use <span class="nm">kcgi</span>, you'll need a web server
capable of running CGI or FastCGI.
<a href="https://httpd.apache.org/">Apache</a>,
<a href="https://nginx.org">nginx</a>, and OpenBSD's
<a href="https://man.openbsd.org/httpd.8">httpd(8)</a>
have all been used extensively, with the latter two natively over
FastCGI and via the <code>slowcgi</code> wrapper.
</p>
<p>
To compile <span class="nm">kcgi</span> applications, use the
package configuration. Linking is similarly normative.
</p>
<pre class="prettyprint">% cc `pkg-config --cflags kcgi` -c yourprog.c
% cc yourprog.o `pkg-config --libs kcgi`</pre>
<p>
Well-deployed web servers, such as the default
<a href="https://www.openbsd.org">OpenBSD</a> server, by default are
deployed within a
<a href="https://man.openbsd.org/chroot.2">chroot(2)</a>. If this
is the case, you'll need to statically link your binary (or pull all
shared libraries into the shadow root).
</p>
<pre class="prettyprint">% cc -static yourprog.o `pkg-config --static --libs kcgi`</pre>
<p>
FastCGI applications may either be started directly by the web
server (which is popular with
<a href="https://httpd.apache.org/">Apache</a>) or <q>externally</q>
given a socket and <a href="kfcgi.8.html">kfcgi(8)</a> (this method
is normative for OpenBSD's
<a href="https://man.openbsd.org/httpd.8">httpd(8)</a> and suggested
for the security precautions taken by the wrapper).
</p>
</section>
<section id="usage" class="text">
<h2>
documentation
</h2>
<p>
The <span class="nm">kcgi</span> manpages, starting with
<a href="kcgi.3.html">kcgi(3)</a>, are the canonical source of
documentation. The following is a list of all manpages:
</p>
<nav id="mannav" data-sblg-nav="1" data-sblg-navtag="man" data-sblg-navcontent="1"
data-sblg-navsort="title">
<a href="${sblg-stripbase}.html">${sblg-title}</a>
</nav>
<p>
Alternatively, the following are introductory materials to the
system:
</p>
<nav data-sblg-nav="1" data-sblg-navtag="tutorial" data-sblg-navcontent="1"
data-sblg-navsort="rdate">
<h3><a href="${sblg-base}.html">${sblg-titletext}</a></h3>
${sblg-aside}
</nav>
<p>
In addition to these resources, the following conference sessions
have referenced <span class="nm">kcgi</span>.
</p>
<ul>
<li>
Dzonsons, Kristaps.
<i>Role-based Access Control in BCHS Web Applications</i>.
Proceedings of
<a href="https://2018.asiabsdcon.org">AsiaBSDCon</a>, Tokyo,
Japan, March 2018.
(<a href="https://kristaps.bsd.lv/absdcon2018">Slides</a>,
<a href="https://www.youtube.com/watch?v=FzF9e4jrnJ4">video</a>.)
<a href="https://kristaps.bsd.lv/absdcon2018/paper.pdf">paper</a>.)
</li>
<li>
Dzonsons, Kristaps.
<i>Secure BSD Web Applications in C: Practical Strategies</i>.
Proceedings of
<a href="https://2017.asiabsdcon.org">AsiaBSDCon</a>, Tokyo,
Japan, March 2017.
(<a href="https://kristaps.bsd.lv/absdcon2017">Slides</a>.)
</li>
<li>
Dzonsons, Kristaps.
<i>Secure BSD Web Application Development in C</i>.
Proceedings of
<a href="https://2016.asiabsdcon.org">AsiaBSDCon</a>,
Tokyo, Japan, March 2016.
(<a href="https://kristaps.bsd.lv/absdcon2016">Slides</a>.)
</li>
<li>
Dzonsons, Kristaps.
<i>kcgi: securing CGI applications in C</i>.
Proceedings of
<a href="https://2015.asiabsdcon.org">AsiaBSDCon</a>,
Tokyo, Japan, March 2015.
(<a href="https://kristaps.bsd.lv/absdcon2015/talk.pdf">Slides</a>,
<a href="https://kristaps.bsd.lv/absdcon2015/paper.pdf">paper</a>.)
</li>
</ul>
<p>
And the following relate to extending standards:
</p>
<ul>
<li>
Dzonsons, Kristaps.
<a href="extending01.html">FastCGI Extensions for Management Control</a>.
March 2016.
</li>
</ul>
</section>
<section id="features" class="text">
<h2>
implementation details
</h2>
<p>
The bulk of <span class="nm">kcgi</span>'s CGI handling lies in
<a href="khttp_parse.3.html">khttp_parse(3)</a>, which fully parses
the HTTP request. Application developers must invoke this function
before all others. For FastCGI, this function is split between
<a href="khttp_fcgi_init.3.html">khttp_fcgi_init(3)</a>, which
initialises context; and
<a href="khttp_fcgi_parse.3.html">khttp_fcgi_parse(3)</a>, which
receives new parsed requests. In either case, requests must be
freed by
<a href="khttp_free.3.html">khttp_free(3)</a>.
</p>
<p>
All functions isolate the parsing and validation of untrusted
network data within a <i>sandboxed</i> child process. Sandboxes
limit the environment available to a process, so exploitable errors
in the parsing process (or validation with third-party libraries)
cannot touch the system environment. This parsed data is returned
to the parent process over a socket. In the following, the <i>HTTP
parser</i> and <i>input validator</i> manage a single HTTP request,
while <i>connection delegator</i> accepts new HTTP requests and
passes them along.
</p>
<figure>
<img src="figure1.svg" alt="Implementation Details" />
<img src="figure4.svg" alt="Implementation Details" />
</figure>
<p>
This method of sandboxing the untrusted parsing process follows
<a href="http://www.openssh.org">OpenSSH</a>, and requires special
handling for each operating system:
</p>
<dl>
<dt>
<a href="http://man7.org/linux/man-pages/man2/seccomp.2.html">seccomp(2)</a>
(Linux)
</dt>
<dd>
This requires a seccomp-enabled Linux kernel and a
recognised hardware architecture. It is supplemented by
<code>setrlimit(2)</code> limiting.
</dd>
<dt>
<a href="https://man.openbsd.org/pledge.2">pledge(2)</a>
(<a href="https://www.openbsd.org">OpenBSD</a>)
</dt>
<dd>
This will only work on OpenBSD >5.8.
</dd>
<dt>
<a href="https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man3/sandbox_init.3.html">sandbox_init(3)</a>
(<a href="https://www.apple.com/osx/">Apple OSX</a>)
</dt>
<dd>
This uses the sandboxing profile for <q>pure computation</q>
as provided in Mac OS X Leopard and later. This is
supplemented by resource limiting via
<code>setrlimit(2)</code>.
</dd>
<dt>
<a href="https://www.freebsd.org/cgi/man.cgi?query=capsicum&sektion=4">capsicum(4)</a>
(<a href="https://www.freebsd.org">FreeBSD</a>)
</dt>
<dd>
Uses the capabilities facility on
<a href="https://www.freebsd.org">FreeBSD</a> 10 and later.
This is supplemented by resource limiting with
<code>setrlimit(2)</code>.
</dd>
</dl>
<p>
Since validation occurs within the sandbox, special care must be
taken that validation routines don't access the environment (e.g.,
by opening files, network connections, etc.), as the child
<strong>might</strong> be abruptly killed by the sandbox facility.
(Not all sandboxes do this.) If required, this kind of validation
can take place after the parse validation sequence.
</p>
<p>
The connection delegator is similar, but has different sandboxing
rules, as it must manage an open socket connection and respond to
new requests.
</p>
</section>
<section class="text">
<h2 id="testing">
testing
</h2>
<p>
<span class="nm">kcgi</span> is shipped with a fully automated
testing framework executed with <code>make regress</code>. To test
your own applications, use the
<a href="kcgiregress.3.html">kcgiregress(3)</a> library.
This framework acts as a mini-webserver, listening on a local port,
translating an HTTP document into a minimal CGI request, and passing
the request to a <span class="nm">kcgi</span> CGI client. For
internal tests, test requests are constructed with
<a href="https://curl.haxx.se/libcurl/">libcurl</a>.
The binding local port is fixed: if you plan on running the
regression suite, you may need to tweak its access port.
</p>
<p>
The testing framework is used by the GitHub
<a href="https://github.com/kristapsdz/kcgi/actions">Actions</a>
to make sure that all commits pass the regression tests on all
supported systems (including with compiler sanitisation).
</p>
<p>
Another testing framework exists for use with the
<a href="http://lcamtuf.coredump.cx/afl/">American fuzzy lop</a>.
To use this, you'll need to compile the <code>make afl</code> target
with your compiler of choice, e.g., <code>make clean</code>, then
<code>make afl CC=afl-gcc</code>. Then run the
<code>afl-fuzz</code> tool on the <code>afl-multipart</code>,
<code>afl-plain</code>, and <code>afl-urlencoded</code> binaries
using the test cases (and dictionaries, for the first) provided.
</p>
</section>
<section class="text">
<h2>
performance
</h2>
<p>
Security comes at a price—but not a stiff price. By design,
<span class="nm">kcgi</span> incurs overhead in three ways: first,
spawning a child to process the untrusted network data; second,
enacting the sandbox framework; and third, passing parsed pairs back
to the parent context. In the case of running CGI scripts,
<span class="nm">kcgi</span> performance is bound to the operating
system's ability to spawn and reap processes.
For FastCGI, the bottleneck becomes the transfer of data.
</p>
</section>
<footer>
<div>
©
<a rel="author" href="https://github.com/kristapsdz">Kristaps Dzonsons</a>
</div>
</footer>
</body>
</html>