Debian Codegraph @

www/bindptr.html

raw
1 <!DOCTYPE html>
2 <html><head>
3 <meta name="viewport" content="width=device-width, initial-scale=1.0">
4 <meta http-equiv="content-type" content="text/html; charset=UTF-8">
5 <link href="sqlite.css" rel="stylesheet">
6 <title>Pointer Passing Interfaces</title>
7 <!-- path= -->
8 </head>
9 <body>
10 <div class=nosearch>
11 <a href="index.html">
12 <img class="logo" src="images/sqlite370_banner.gif" alt="SQLite" border="0">
13 </a>
14 <div><!-- IE hack to prevent disappearing logo --></div>
15 <div class="tagline desktoponly">
16 Small. Fast. Reliable.<br>Choose any three.
17 </div>
18 <div class="menu mainmenu">
19 <ul>
20 <li><a href="index.html">Home</a>
21 <li class='mobileonly'><a href="javascript:void(0)" onclick='toggle_div("submenu")'>Menu</a>
22 <li class='wideonly'><a href='about.html'>About</a>
23 <li class='desktoponly'><a href="docs.html">Documentation</a>
24 <li class='desktoponly'><a href="download.html">Download</a>
25 <li class='wideonly'><a href='copyright.html'>License</a>
26 <li class='desktoponly'><a href="support.html">Support</a>
27 <li class='desktoponly'><a href="prosupport.html">Purchase</a>
28 <li class='search' id='search_menubutton'>
29 <a href="javascript:void(0)" onclick='toggle_search()'>Search</a>
30 </ul>
31 </div>
32 <div class="menu submenu" id="submenu">
33 <ul>
34 <li><a href='about.html'>About</a>
35 <li><a href='docs.html'>Documentation</a>
36 <li><a href='download.html'>Download</a>
37 <li><a href='support.html'>Support</a>
38 <li><a href='prosupport.html'>Purchase</a>
39 </ul>
40 </div>
41 <div class="searchmenu" id="searchmenu">
42 <form method="GET" action="search">
43 <select name="s" id="searchtype">
44 <option value="d">Search Documentation</option>
45 <option value="c">Search Changelog</option>
46 </select>
47 <input type="text" name="q" id="searchbox" value="">
48 <input type="submit" value="Go">
49 </form>
50 </div>
51 </div>
52 <script>
53 function toggle_div(nm) {
54 var w = document.getElementById(nm);
55 if( w.style.display=="block" ){
56 w.style.display = "none";
57 }else{
58 w.style.display = "block";
59 }
60 }
61 function toggle_search() {
62 var w = document.getElementById("searchmenu");
63 if( w.style.display=="block" ){
64 w.style.display = "none";
65 } else {
66 w.style.display = "block";
67 setTimeout(function(){
68 document.getElementById("searchbox").focus()
69 }, 30);
70 }
71 }
72 function div_off(nm){document.getElementById(nm).style.display="none";}
73 window.onbeforeunload = function(e){div_off("submenu");}
74 /* Disable the Search feature if we are not operating from CGI, since */
75 /* Search is accomplished using CGI and will not work without it. */
76 if( !location.origin || !location.origin.match || !location.origin.match(/http/) ){
77 document.getElementById("search_menubutton").style.display = "none";
78 }
79 /* Used by the Hide/Show button beside syntax diagrams, to toggle the */
80 function hideorshow(btn,obj){
81 var x = document.getElementById(obj);
82 var b = document.getElementById(btn);
83 if( x.style.display!='none' ){
84 x.style.display = 'none';
85 b.innerHTML='show';
86 }else{
87 x.style.display = '';
88 b.innerHTML='hide';
89 }
90 return false;
91 }
92 var antiRobot = 0;
93 function antiRobotGo(){
94 if( antiRobot!=3 ) return;
95 antiRobot = 7;
96 var j = document.getElementById("mtimelink");
97 if(j && j.hasAttribute("data-href")) j.href=j.getAttribute("data-href");
98 }
99 function antiRobotDefense(){
100 document.body.onmousedown=function(){
101 antiRobot |= 2;
102 antiRobotGo();
103 document.body.onmousedown=null;
104 }
105 document.body.onmousemove=function(){
106 antiRobot |= 2;
107 antiRobotGo();
108 document.body.onmousemove=null;
109 }
110 setTimeout(function(){
111 antiRobot |= 1;
112 antiRobotGo();
113 }, 100)
114 antiRobotGo();
115 }
116 antiRobotDefense();
117 </script>
118 <div class=fancy>
119 <div class=nosearch>
120 <div class="fancy_title">
121 Pointer Passing Interfaces
122 </div>
123 <div class="fancy_toc">
124 <a onclick="toggle_toc()">
125 <span class="fancy_toc_mark" id="toc_mk">&#x25ba;</span>
126 Table Of Contents
127 </a>
128 <div id="toc_sub"><div class="fancy-toc1"><a href="#overview">1. Overview</a></div>
129 <div class="fancy-toc1"><a href="#a_brief_history_of_pointer_passing_in_sqlite">2. A Brief History Of Pointer Passing In SQLite</a></div>
130 <div class="fancy-toc2"><a href="#upping_the_threat_level">2.1. Upping The Threat Level</a></div>
131 <div class="fancy-toc2"><a href="#preventing_forged_pointers">2.2. Preventing Forged Pointers</a></div>
132 <div class="fancy-toc2"><a href="#pointer_leaks">2.3. Pointer Leaks</a></div>
133 <div class="fancy-toc1"><a href="#the_new_pointer_passing_interfaces">3. The New Pointer-Passing Interfaces</a></div>
134 <div class="fancy-toc2"><a href="#pointer_types">3.1. Pointer Types</a></div>
135 <div class="fancy-toc3"><a href="#pointer_types_are_static_strings">3.1.1. Pointer types are static strings</a></div>
136 <div class="fancy-toc2"><a href="#destructor_functions">3.2. Destructor Functions</a></div>
137 <div class="fancy-toc1"><a href="#restrictions_on_the_use_of_pointer_values">4. Restrictions On The Use of Pointer Values</a></div>
138 <div class="fancy-toc1"><a href="#summary">5. Summary</a></div>
139 </div>
140 </div>
141 <script>
142 function toggle_toc(){
143 var sub = document.getElementById("toc_sub")
144 var mk = document.getElementById("toc_mk")
145 if( sub.style.display!="block" ){
146 sub.style.display = "block";
147 mk.innerHTML = "&#x25bc;";
148 } else {
149 sub.style.display = "none";
150 mk.innerHTML = "&#x25ba;";
151 }
152 }
153 </script>
154 </div>
155
156
157
158
159
160 <h1 id="overview"><span>1. </span>Overview</h1>
161
162 <p>
163 Three new "_pointer()" interfaces were added to SQLite 3.20.0 (2017-08-01):
164 </p><ul>
165 <li> <a href="c3ref/bind_blob.html">sqlite3_bind_pointer()</a>,
166 </li><li> <a href="c3ref/result_blob.html">sqlite3_result_pointer()</a>, and
167 </li><li> <a href="c3ref/value_blob.html">sqlite3_value_pointer()</a>.
168 </li></ul>
169
170 <p>Questions and confusion quickly arose
171 on the <a href="support.html#mailinglists">mailing lists</a> about the purpose behind these new interfaces,
172 why they were introduced, and what problem they solve. This essay attempts
173 to answer those questions and clear up the confusion.
174
175 </p><h1 id="a_brief_history_of_pointer_passing_in_sqlite"><span>2. </span>A Brief History Of Pointer Passing In SQLite</h1>
176
177 <p>
178 It is sometimes convenient for SQLite extensions to
179 communicate non-SQL values between subcomponents or between the extension
180 and the application. Some examples:
181
182 </p><ul>
183 <li><p>
184 In the <a href="fts3.html">FTS3</a> extension, the
185 <a href="fts3.html#full_text_index_queries">MATCH operator</a> (which does the full-text search)
186 needs to communicate details of matching entries
187 to the <a href="fts3.html#snippet">snippet()</a>, <a href="fts3.html#offsets">offsets()</a>,
188 and <a href="fts3.html#matchinfo">matchinfo()</a> functions so that those functions can convert the details
189 of the match into useful output.
190
191 </p></li><li><p>
192 In order for an application to
193 <a href="fts5.html#extending_fts5">add new extensions to FTS5</a>, such as new tokenizers,
194 the application needs a pointer to the "fts5_api" object.
195
196 </p></li><li><p>
197 In the <a href="carray.html">CARRAY extension</a>, the application needs to tell the
198 extension the
199 location of a C-language array that contains the data for the table-valued
200 function that the extension implements.
201 </p></li></ul>
202
203 <p>
204 The traditional way of communicating this information was to transform a
205 C-language pointer into a BLOB or a 64-bit integer, then move that BLOB or
206 integer through SQLite using the usual interfaces like
207 <a href="c3ref/bind_blob.html">sqlite3_bind_blob()</a>, <a href="c3ref/result_blob.html">sqlite3_result_blob()</a>, <a href="c3ref/value_blob.html">sqlite3_value_blob()</a> or
208 the integer equivalents.
209
210 </p><h2 id="upping_the_threat_level"><span>2.1. </span>Upping The Threat Level</h2>
211
212 <p>
213 Passing around pointers as if they were integers or BLOBs is easy,
214 effective, and works well in an environment where the application
215 components are all friendly toward one another. However, passing pointers
216 as integers and BLOBs allows hostile SQL text to forge invalid pointers that
217 can carry out mischief.
218
219 </p><p>
220 For example, the first argument to the <a href="fts3.html#snippet">snippet()</a> function is supposed to
221 be a special column of the FTS3 table that contains a pointer to an fts3cursor
222 object that contains information about the current full text search match.
223 That pointer was formerly passed as a BLOB.
224 For example, if the FTS3 table is named "t1" and has a column named "cx",
225 one might write:
226
227 </p><div class="codeblock"><pre>SELECT snippet(t1) FROM t1 WHERE cx MATCH $pattern;
228 </pre></div>
229
230 <p>
231 But if a hacker is able to run arbitrary SQL, he might run a slightly
232 different query, like this:
233
234 </p><div class="codeblock"><pre>SELECT hex(t1) FROM t1 WHERE cx MATCH $pattern;
235 </pre></div>
236
237 <p>
238 Because the pointer is passed in the t1 column of the t1
239 table as a BLOB (in older versions of SQLite), such a query would have
240 shown the value of the
241 pointer in hex. The attacker could then modify that pointer to try to
242 get the snippet() function to modify memory in some other part of
243 the application address space instead of the fts3cursor object it
244 was supposed to be operating on:
245
246 </p><div class="codeblock"><pre>SELECT snippet(x'6092310100000000') FROM t1 WHERE cx MATCH $pattern;
247 </pre></div>
248
249 <p>
250 Historically, this was not considered a threat. The argument was that if
251 a hostile agent is able to inject arbitrary SQL text into the application,
252 then that agent is already in full control of the application, so
253 letting the hostile agent forge a pointer does not give the agent
254 any new capability.
255
256 </p><p>
257 For most cases, it is true that potential attackers have no way of injecting
258 arbitrary SQL, and so most uses of SQLite are immune to the attack above.
259 But there are some notable exceptions. To wit:
260
261 </p><ul>
262 <li><p>
263 The <a href="https://en.wikipedia.org/wiki/Web_SQL_Database">WebSQL</a> interface
264 to webkit allowed any webpage to run arbitrary SQL in the browser
265 for Chrome and Safari. That arbitrary SQL was supposed to be run inside
266 a sandbox where it could do no harm even if exploited, but that sandbox
267 turned out to be less secure than people supposed. In the spring of 2017,
268 one team of hackers was able to root an iMac using a long sequence of
269 exploits, one of which involved corrupting the pointers passed as BLOB
270 values to the snippet() FTS3 function of an SQLite database running via
271 the WebSQL interface inside of Safari.
272
273 </p></li><li><p>
274 On Android, we are told, there are many services that will blindly
275 run arbitrary SQL that is passed to them by untrustworthy apps
276 that have been downloaded from dodgy corners of the internet.
277 Android services are suppose to be more guarded about running SQL
278 from unvetted sources. This author does not have any specific examples
279 to the contrary, but he has heard rumors that they exist. Even if
280 all Android services are more careful and properly vet all the SQL
281 they run, it would be difficult to audit them
282 all in order to verify that they are safe. Hence, security-minded people
283 are keen to ensure that no exploits are possible by passing arbitrary
284 SQL text.
285
286 </p></li><li><p>
287 The <a href="https://www.fossil-scm.org/">Fossil</a> version control system (designed
288 and written for the purpose of supporting SQLite development) allows
289 mildly trusted users to enter arbitrary SQL for generating trouble-ticket
290 reports. That SQL is sanitized using the
291 <a href="c3ref/set_authorizer.html">sqlite3_set_authorizer()</a> interface, and no exploits have ever been
292 found. But this is an example of potentially hostile agents being able to
293 inject arbitrary SQL into the system.
294 </p></li></ul>
295
296 <h2 id="preventing_forged_pointers"><span>2.2. </span>Preventing Forged Pointers</h2>
297
298 <p>
299 The first attempt at closing security gaps in pointer passing was to
300 prevent pointer values from being forged. This was accomplished by
301 having the sender attach a subtype to each pointer using
302 <a href="c3ref/result_subtype.html">sqlite3_result_subtype()</a> and having the receiver verify that subtype
303 using <a href="c3ref/value_subtype.html">sqlite3_value_subtype()</a> and reject pointers that had an incorrect
304 subtype. Since there is no way to attach a subtype to a result using
305 pure SQL, this prevents pointers from being forged using SQL. The only
306 way to send a pointer is to use C code. If an attacker can set a subtype,
307 then he is also able to forge a pointer without the help of SQLite.
308
309 </p><p>
310 Using subtypes to identify valid pointers prevented the WebSQL exploit.
311 But it turned out to be an incomplete solution.
312
313 <a name="ptrleak"></a>
314
315 </p><h2 id="pointer_leaks"><span>2.3. </span>Pointer Leaks</h2>
316
317 <p>
318 The use of subtypes on pointers prevented pointer forgery using
319 pure SQL. But subtypes do nothing to prevent an attacker from reading
320 the values of pointers. In other words, subtypes on pointer values
321 prevent attacks using SQL statements like this:
322
323 </p><div class="codeblock"><pre>SELECT snippet(x'6092310100000000') FROM t1 WHERE cx MATCH $pattern;
324 </pre></div>
325
326 <p>
327 The BLOB argument to snippet() does not have the correct subtype, so the
328 snippet function ignores it, makes no changes to any data structures,
329 and harmlessly returns NULL.
330
331 </p><p>
332 But the use of subtypes does nothing to prevent the value of a
333 pointer from being read using SQL code like this:
334
335 </p><div class="codeblock"><pre>SELECT hex(t1) FROM t1 WHERE cx MATCH $pattern;
336 </pre></div>
337
338 <p>
339 What harm could come of that, you ask? The SQLite developers (including
340 this author) wondered the same thing. But then security researchers
341 pointed out that knowledge of pointers can help attackers to circumvent
342 address-space randomization defenses. This is called a "pointer leak".
343 A pointer leak is not itself a vulnerability, but it can aid an attacker
344 in effectively exploiting other vulnerabilities.
345
346 </p><h1 id="the_new_pointer_passing_interfaces"><span>3. </span>The New Pointer-Passing Interfaces</h1>
347
348 <p>
349 Allowing extension components to pass private information to one another
350 securely and without introducing pointer leaks requires new interfaces:
351
352 </p><ul>
353 <li><b><a href="c3ref/bind_blob.html">sqlite3_bind_pointer</a>(S,I,P,T,D)</b> &rarr;
354 Bind pointer P of type T to the I-th parameter of prepared statement S.
355 D is an optional destructor function for P.
356 </li><li><b><a href="c3ref/result_blob.html">sqlite3_result_pointer</a>(C,P,T,D)</b> &rarr;
357 Return pointer P of type T as the argument of function C.
358 D is an optional destructor function for P.
359 </li><li><b><a href="c3ref/value_blob.html">sqlite3_value_pointer</a>(V,T)</b> &rarr;
360 Return the pointer of type T associated with value V, or if V has no
361 associated pointer, or if the pointer on V is of a type different from
362 T, then return NULL.
363 </li></ul>
364
365 <p>
366 To SQL, the values created by <a href="c3ref/bind_blob.html">sqlite3_bind_pointer()</a> and
367 <a href="c3ref/result_blob.html">sqlite3_result_pointer()</a> are indistinguishable from NULL. An
368 SQL statement that tries to use the <a href="lang_corefunc.html#hex">hex()</a> function to read the
369 value of a pointer will get an SQL NULL answer. The only way to
370 discover whether or not a value has an associated pointer is to
371 use the <a href="c3ref/value_blob.html">sqlite3_value_pointer()</a> interface with the appropriate
372 type string T.
373
374 </p><p>
375 Pointer values read by <a href="c3ref/value_blob.html">sqlite3_value_pointer()</a>
376 cannot be generated by pure SQL. Hence, it is not possible for SQL to
377 forge pointers.
378
379 </p><p>
380 Pointer values generated by <a href="c3ref/bind_blob.html">sqlite3_bind_pointer()</a> and
381 <a href="c3ref/result_blob.html">sqlite3_result_pointer()</a> cannot be read by pure SQL.
382 Hence, it is not possible for SQL to leak the value of pointers.
383
384 </p><p>
385 In this way the new pointer-passing interface seems to solve all of the
386 security problems associated with passing pointer values from one
387 extension to another in SQLite.
388
389 <a name="ptrtyp"></a>
390
391 </p><h2 id="pointer_types"><span>3.1. </span>Pointer Types</h2>
392
393 <p>
394 The "pointer type" in the last parameter to <a href="c3ref/bind_blob.html">sqlite3_bind_pointer()</a>,
395 <a href="c3ref/result_blob.html">sqlite3_result_pointer()</a>, and <a href="c3ref/value_blob.html">sqlite3_value_pointer()</a> is used to prevent
396 pointers intended for one extension from being redirected to a different
397 extension. For example, without the use of pointer types, an attacker
398 could still get access to pointer information in a system that included
399 both the <a href="fts3.html">FTS3</a> and the <a href="carray.html">CARRAY extension</a> using SQL like this:
400
401 </p><div class="codeblock"><pre>SELECT ca.value FROM t1, carray(t1,10) AS ca WHERE cx MATCH $pattern
402 </pre></div>
403
404 <p>
405 In the statement above, the FTS3 cursor pointer generated by the
406 MATCH operator is send into the carray() table-valued function instead
407 of its intended recipient snippet(). The carray() function treats the
408 pointer as a pointer to an array of integers and returns each integer
409 one by one, thus leaking the content of the FTS3 cursor object. Since
410 the FTS3 cursor object contains pointers to other objects, the statement
411 above would be a pointer leak.
412
413 </p><p>
414 Except, the statement above does not work, thanks to pointer types.
415 The pointer generated by the MATCH operator has a type of "fts3cursor"
416 but the carray() function expects to receives a pointer of type "carray".
417 Because the pointer type on the <a href="c3ref/result_blob.html">sqlite3_result_pointer()</a> does not match
418 the pointer type on the <a href="c3ref/value_blob.html">sqlite3_value_pointer()</a> call,
419 <a href="c3ref/value_blob.html">sqlite3_value_pointer()</a> returns NULL in carray() and thus signals
420 the CARRAY extension that it has been passed an invalid pointer.
421
422 </p><h3 id="pointer_types_are_static_strings"><span>3.1.1. </span>Pointer types are static strings</h3>
423
424 <p>
425 Pointer types are static strings, which ideally should be string literals
426 embedded directly in the SQLite API call, not parameters passed in from
427 other functions. Consideration was given to using integer values as
428 the pointer type, but static strings provides a much larger name space
429 which reduces the chance of accidental type-name collisions between
430 unrelated extensions.
431
432 </p><p>
433 By "static string", we mean a zero-terminated array of bytes that is
434 fixed and unchanging for the life of the program. In other words, the
435 pointer type string should be a string constant.
436 In contrast, a "dynamic string" is a zero-terminated array of bytes
437 that is held in memory allocated
438 from the heap, and which must be freed to avoid a memory leak.
439 Do not use dynamic strings as the pointer type string.
440
441 </p><p>
442 Multiple commentators have expressed a desire to use dynamic strings
443 for the pointer type, and to have SQLite take ownership of the type strings
444 and to automatically free the type string
445 when it has finished using it. That design is rejected for the
446 following reasons:
447
448 </p><ol>
449 <li><p>
450 The pointer type is not intended to be flexible and dynamic. The
451 pointer type is intended to be a design-time constant. Applications
452 should not synthesize pointer type strings at run-time. Providing
453 support for dynamic pointer type strings would lead developers
454 to misuse the pointer-passing interfaces by creating run-time
455 synthesized pointer type strings. Requiring the pointer type strings
456 to be static encourages developers to do the right thing by choosing
457 fixed pointer type names at design-time and encoding those names
458 as constant strings.
459
460 </p></li><li><p>
461 All string values at the SQL level in SQLite are dynamic strings.
462 Requiring type strings to be static makes it difficult to
463 create an application-defined SQL function that
464 can synthesize a pointer of an arbitrary type. We do not want users
465 to create such SQL functions, since such functions would compromise the
466 security of the system. Thus, the requirement to use static strings
467 helps to defend that the integrity of the pointer-passing interfaces against
468 ill-designed SQL functions. The static string requirement is not
469 a perfect defense, since a sophisticated programmer can code around
470 it, and a novice program can simply take the memory leak. But by
471 stating that the pointer type string must be static, we hope to encourage
472 developers who might otherwise use a dynamic string for the pointer type
473 to think more carefully about the problem and avoid introducing
474 security issues.
475
476 </p></li><li><p>
477 Having SQLite take ownership of the type strings would impose a performance
478 cost on all applications, even applications that do not use the
479 pointer-passing interfaces. SQLite passes values around as instances
480 of the <a href="c3ref/value.html">sqlite3_value</a> object. That object has a destructor, which because
481 of the fact that sqlite3_value objects are used for nearly everything, is
482 invoked frequently. If the destructor needs to check to see if there is
483 a pointer type string that needs to be freed, that is a few extra CPU
484 cycles that need to be burned on each call to the destructor. Those
485 cycles add up. We would be willing to bear the cost of the extra CPU
486 cycles if pointer-passing was a commonly used programming paradigm, but
487 pointer-passing is rare, and so it seems unwise to impose a run-time cost
488 on billions and billions of applications that do not use pointer passing
489 just for convenience of a few applications that do.
490 </p></li></ol>
491
492 <p>
493 If you feel that you need dynamic pointer type strings in your application,
494 that is a strong indicator that you are misusing the pointer-passing interface.
495 Your intended use may be unsafe.
496 Please rethink your design. Determine if you really need to be passing
497 pointers through SQL in the first place. Or perhaps find a different
498 mechanism other than the pointer-passing interfaces described by this
499 article.
500
501 </p><h2 id="destructor_functions"><span>3.2. </span>Destructor Functions</h2>
502
503 <p>
504 The last parameter to the <a href="c3ref/bind_blob.html">sqlite3_bind_pointer()</a> and
505 <a href="c3ref/result_blob.html">sqlite3_result_pointer()</a> routines is a pointer to a procedure
506 used to dispose of the P pointer once SQLite has finished with it.
507 This pointer can be NULL, in which case no destructor is called.
508
509 </p><p>
510 When the D parameter is not NULL, that means that ownership of the
511 pointer is being transferred to SQLite. SQLite will take responsibility
512 for freeing resources associated with the pointer when it has finished
513 using the pointer. If the D parameter is NULL, that means that ownership
514 of the pointer remains with the caller and the caller is responsible for
515 disposing of the pointer.
516
517 </p><p>
518 Note that the destructor function D is for the pointer value P, not for
519 the type string T. The type string T should be a static string with an
520 infinite lifetime.
521
522 </p><p>
523 If ownership of the pointer is passed into SQLite by providing a
524 non-NULL D parameter to <a href="c3ref/bind_blob.html">sqlite3_bind_pointer()</a> or <a href="c3ref/result_blob.html">sqlite3_result_pointer()</a>
525 then the ownership remains with SQLite until the object is destroyed.
526 There is no way to transfer ownership out of SQLite and back into the
527 application again.
528
529 </p><h1 id="restrictions_on_the_use_of_pointer_values"><span>4. </span>Restrictions On The Use of Pointer Values</h1>
530
531 <p>
532 The pointers that piggy-back on SQL NULL values using the
533 <a href="c3ref/bind_blob.html">sqlite3_bind_pointer()</a>, <a href="c3ref/result_blob.html">sqlite3_result_pointer()</a>, and
534 <a href="c3ref/value_blob.html">sqlite3_value_pointer()</a> interface are transient and ephemeral.
535 The pointers are never written into the database. The pointers
536 will not survive sorting. The latter fact is why there is no
537 sqlite3_column_pointer() interface, since it is impossible to
538 predict whether or not the query planner will insert a sort operation
539 prior to returning a value from a query, so it would be impossible to
540 know if a pointer value inserted into a query by
541 <a href="c3ref/bind_blob.html">sqlite3_bind_pointer()</a> or <a href="c3ref/result_blob.html">sqlite3_result_pointer()</a> would survive
542 through to the result set.
543
544 </p><p>
545 Pointer values must flow directly from their producer into their
546 consumer, with no intermediate operators or functions. Any transformation
547 of a pointer value destroys the pointer and transforms the value into
548 an ordinary SQL NULL.
549
550 </p><h1 id="summary"><span>5. </span>Summary</h1>
551
552 <p>Key take-aways from this essay:
553
554 </p><ol>
555 <li><p>
556 The internet is an increasingly hostile place. These day, developers
557 should assume that attackers will find a way to execute arbitrary SQL
558 in an application.
559 Applications should be designed to prevent the execution of arbitrary
560 SQL from escalating into a more severe exploit.
561
562 </p></li><li><p>
563 A few SQLite extensions benefit from passing pointers:
564 </p><ul>
565 <li>The <a href="fts3.html">FTS3</a> MATCH operator passes pointers into <a href="fts3.html#snippet">snippet()</a>,
566 <a href="fts3.html#offsets">offsets()</a>, and <a href="fts3.html#matchinfo">matchinfo()</a>.
567 </li><li>The <a href="carray.html">carray table-valued function</a> needs to accept a pointer to
568 an array of C-language values from the application.
569 </li><li>The <a href="https://sqlite.org/src/file/ext/misc/remember.c">remember() extension</a>
570 needs a pointer to a C-language integer variable in which to remember
571 the value it passes.
572 </li><li>Applications need to receive a pointer to the "fts5_api" object in order
573 to add extensions, such as custom tokenizers, to the <a href="fts5.html">FTS5</a> extension.
574 </li></ul>
575
576 </li><li><p>
577 Pointers should <u>never</u> be exchanged by encoding them as some other
578 SQL datatype, such as integers or BLOBs. Instead, use the interfaces
579 designed to facilitate secure pointer passing:
580 <a href="c3ref/bind_blob.html">sqlite3_bind_pointer()</a>, <a href="c3ref/result_blob.html">sqlite3_result_pointer()</a>, and
581 <a href="c3ref/value_blob.html">sqlite3_value_pointer()</a>.
582
583 </p></li><li><p>
584 The use of pointer-passing is an advanced technique that should be used
585 infrequently and cautiously. Pointer-passing should not be
586 used haphazardly or carelessly. Pointer-passing is a sharp tool
587 that can leave deep scars if misused.
588
589 </p></li><li><p>
590 The "pointer type" string which is the last parameter to each of the
591 pointer-passing interfaces should be a distinct, application-specific
592 string literal that appears directly in the API call. The pointer type
593 should not be a parameter passed in from a higher-level function.
594 </p></li></ol>
595 <p align="center"><small><i>This page last modified on <a href="https://sqlite.org/docsrc/honeypot" id="mtimelink" data-href="https://sqlite.org/docsrc/finfo/pages/bindptr.in?m=0313c34170">2022-10-07 10:23:26</a> UTC </small></i></p>
596