19 comments

  • kentonv40 days ago
    A few years back I patched the memory allocator used by the Cloudflare Workers runtime to overwrite all memory with a static byte pattern on free, so that uninitialized allocations contain nothing interesting.<p>We expected this to hurt performance, but we were unable to measure any impact in practice.<p>Everyone still working in memory-unsafe languages should really just do this IMO. It would have mitigated this Mongo bug.
    • MuffinFlavored40 days ago
      &gt; OpenBSD uses 0xdb to fill newly allocated memory and 0xdf to fill memory upon being freed. This helps developers catch &quot;use-before-initialization&quot; (seeing 0xdb) and &quot;use-after-free&quot; (seeing 0xdf) bugs quickly.<p>Looks like this is the default in OpenBSD.
      • basilgohar39 days ago
        I like this. The only information leaking is whether the memory range was previously used. I suppose you may want to control for that. I&#x27;d be surprised if OpenBSD didn&#x27;t provide a flag to just freed memory to the same value as never allocated.
        • cpach37 days ago
          This makes me curious. This bit of information – knowing whether the memory range was previously used or not – how could it be exploited?
    • amomchilov40 days ago
      Recent macOS versions zero out memory on free, which improves the efficacy of memory compression. Apparently it’s a net performance gain in the average case
      • LoganDark39 days ago
        I wonder if Apple Silicon has hardware acceleration for memory zeroing... Knowing Apple, I wouldn&#x27;t be surprised.
        • chuckadams39 days ago
          ARM in general does, or at least some modern variants. Various docs for Android and LLVM suggest it&#x27;s part of the Memory Tagging Extension.
    • cperciva40 days ago
      <i>A few years back I patched the memory allocator used by the Cloudflare Workers runtime to overwrite all memory with a static byte pattern on free, so that uninitialized allocations contain nothing interesting.</i><p>Note that many malloc implementations will do this for you given an appropriate environment, e.g. setting MALLOC_CONF to opt.junk=free will do this on FreeBSD.
    • dmitrygr40 days ago
      FYI, at least in C&#x2F;C++, the compiler is free to throw away assignments to any memory pointed to by a pointer if said pointer is about to be passed to free(), so depending on how you did this, no perf impact could have been because your compiler removed the assignment. This will even affect a call to memset()<p>see here: <a href="https:&#x2F;&#x2F;godbolt.org&#x2F;z&#x2F;rMa8MbYox" rel="nofollow">https:&#x2F;&#x2F;godbolt.org&#x2F;z&#x2F;rMa8MbYox</a>
      • kentonv40 days ago
        I patched the free() implementation itself, not the code that calls free().<p>I did, of course, test it, and anyway we now run into the &quot;freed memory&quot; pattern regularly when debugging (yes including optimized builds), so it&#x27;s definitely working.
      • shakna40 days ago
        However, if you recast to volatile, the compiler will keep it:<p><pre><code> #include &lt;stdlib.h&gt; #include &lt;string.h&gt; void free(void* ptr); void not_free(void* ptr); void test_with_free(char* ptr) { ptr[5] = 6; void *(* volatile memset_v)(void *s, int c, size_t n) = memset; memset_v(ptr + 2, 3, 4); free(ptr); } void test_with_other_func(char* ptr) { ptr[5] = 6; void *(* volatile memset_v)(void *s, int c, size_t n) = memset; memset_v(ptr + 2, 3, 4); not_free(ptr); }</code></pre>
        • cperciva40 days ago
          That code is not guaranteed to work. Declaring memset_v as volatile means that the variable has to be read, but does not imply that the function must be called; the compiler is free to compile the function call as &quot;tmp = memset_v; if (tmp != memset) tmp(...)&quot; relying on its knowledge that in the likely case of equality the call can be optimized away.
          • shakna40 days ago
            Whilst the C standard doesn&#x27;t guarantee it, both LLVM and GCC _do_. They have implementation-defined that it will work, so are not free to optimise it away.<p>[0] <a href="https:&#x2F;&#x2F;llvm.org&#x2F;docs&#x2F;LangRef.html#llvm-memset-intrinsics" rel="nofollow">https:&#x2F;&#x2F;llvm.org&#x2F;docs&#x2F;LangRef.html#llvm-memset-intrinsics</a><p>[1] <a href="https:&#x2F;&#x2F;gitweb.git.savannah.gnu.org&#x2F;gitweb&#x2F;?p=gnulib.git;a=blob_plain;f=lib&#x2F;memset_explicit.c;hb=refs&#x2F;heads&#x2F;stable-202301" rel="nofollow">https:&#x2F;&#x2F;gitweb.git.savannah.gnu.org&#x2F;gitweb&#x2F;?p=gnulib.git;a=b...</a>
            • sitkack39 days ago
              Relying on implementation behavior is the perfect way to introduce a hidden in plain site vulnerability.
              • 11223338 days ago
                Most of C++ programs written before P0593R6 depended on implementation behaviour, and were graciously allowed to not be undefined behaviour just 5 years ago. C++ as a language standard is mostly irrelevant, what one should care about is what the compiler authors consider valid code.
              • shakna39 days ago
                Using pragmas, attributes and optimisation guarantees is the point of implementation-defined behaviour in the first place.<p>The Linux kernel extensively uses gcc extensions. That doesn&#x27;t inherently make it insecure.
              • CJefferson39 days ago
                You have to rely on implementation for anything to do with what happens to memory after it is freed, or really almost anything to do with actual bytes in RAM.
            • raverbashing39 days ago
              Yeah the C committee is wrong here
              • uecker39 days ago
                I don&#x27;t see why?<p>The C committee gave you memset_explicit. But note that there is still no guarantee that information can not leak. This is generally a very hard problem as information can leak in many different ways as it may have been copied by the compiler. Fully memory safe languages (so &quot;Safe Rust&quot; but not necessarily real-word Rust) would offer a bit more protection by default, but then there are still side-channel issues.
                • raverbashing39 days ago
                  Because, for the 1384th time, they&#x27;re pretending they can ignore what the programmer explicitly told them to do<p>Creating memset_explicit won&#x27;t fix existing code. &quot;Oh but what if maybe&quot; is just cope.<p>If I do memset then free <i>then that&#x27;s what I want to do</i><p>And the way things go I won&#x27;t be surprised if they break memset_explicit for some other BS reason and then make you use memset_explicit_you_really_mean_it_this_time
                  • uecker39 days ago
                    Your problem is not the C committee but your lack of understanding how optimizing compilers work. WG14 could, of course, specify that a compiler has do exactly what you tell it to do. And in fact, every compiler supports this already: Im most cases even by default! Just do not turn on optimization. But this is not what most people want.<p>Once you accept that optimizing compilers do, well, optimizations, the question is what should be allowed and what not. Both inlining &quot;memset&quot; and eliminating dead stores are both simply optimizations which people generally want.<p>If you want a store not to be eliminated by a compiler, you can make it volatile. The C standard says this can not be deleted by optimizations. The criticism with this was that later undefined behavior could &quot;undo&quot; this by &quot;travelling in time&quot;. We made it clear in ISO C23 that this not allowed (and I believe it never was) - against protests from some compiler folks. Compilers still do not fully conform to this, which shows the limited power WG14 has to change reality.
                    • raverbashing39 days ago
                      Nope it is the C committee<p>&gt; Once you accept that optimizing compilers do, well, optimizations<p>Why in tarnation it is optimizing out a write to a pointer out before a function that takes said pointer? Imagine it is any other function besides free, see how ridiculous that sounds?
                      • rectang39 days ago
                        It&#x27;s been many years since C compilers started making pathological-but-technically-justifiable optimizations that work against the programmer. The problem is the vast sea of &quot;undefined behavior&quot; — if you are not a fully qualified language lawyer versed in every nook and cranny of the C standard, prepare to be surprised.<p>Many of us who don&#x27;t like working under such conditions have just moved on to other languages.
                        • uecker39 days ago
                          I agree that compilers were too aggressive in exploiting UB, but this is not the topic of this thread which has nothing to do with UB. But also the situation with UB is in practice not too bad. While compilers broke some old code which caused frustration, when writing new code most UB can easily be dealt with in practice by following some basic ground rules (e.g. no unsafe casts, being careful with pointer arithmetic) and by activating some compiler flags. It is not anything that should cause much trouble when programming in C.
                      • uecker39 days ago
                        Because it is a dead store. Removing dead stores does not sound ridiculous to me and neither is it to anybody using an optimizing compiler in the last decades.
                      • shakna39 days ago
                        Tree shaking is pretty standard. Optimising out the write sounds fine to me - with the exception of a volatile pointer. That, there, is a mistake.
                        • raverbashing39 days ago
                          Optimizing out a write to (example) an array on the stack seems fine to me.<p>Optimizing out a function call to a heap pointer (<i>especially</i> memset) seems wrong to me. You called the function, it should call the function!<p>But it&#x27;s again the C language saving time not wearing a seatbelt or checking the tire pressure for saving 10s on a 2h trip
                          • fluoridation39 days ago
                            The whole point of the optimizer is that it can detect inefficiencies by treating every statement as some combination of simple, fundamental operations. The compiler is not seeing &quot;call memset() on pointer to heap&quot;, it&#x27;s seeing &quot;write of variable size&quot; just before &quot;deallocation&quot;. For some, optimizing that will be a problem, for others, <i>not</i> optimizing it will leave performance on the table.<p>There are still ways to obtain the desired behavior. Just put a call to a DLL or SO that implements what you need. The compiler cannot inspect the behavior of functions across module boundaries, so it cannot tell whether removing the call preserves semantics or not (for example, it could be that the external function sends the contents of the buffer to a file), so it will not remove it.
                            • uecker39 days ago
                              A modern compiler may also completely remove malloc &#x2F; free pairs and move the computation to the stack. And I do not see what this has to do with C, it should be the same for most languages. C gives you tools to express low-level intent such as &quot;volatile&quot;, but one has to use them.
                              • 11223338 days ago
                                Strong disagree. In C, malloc and free are functions, and I expect no magic to happen when calling a function. If malloc and free were keywords like sizeof, it would have been different.
                                • fluoridation38 days ago
                                  Your problem is that you&#x27;re treating words such as &quot;function&quot; and &quot;call&quot; as if they had meaning outside of the language itself (or, more specifically, outside of the C abstract machine), when the point of the compiler is precisely to melt away the language parts of the specified program and be left with a concrete program that matches its behavior. If you view a binary in a disassembler, you will not find any &quot;functions&quot; or &quot;calls&quot;. Maybe that particular architecture happens to have a &quot;call&quot; instruction to jump to &quot;functions&quot;, but these words are merely homophones with what C refers to as &quot;functions&quot; and &quot;calls&quot;.<p>When you &quot;call&quot; a &quot;function&quot; in the source you&#x27;re not specifying to the compiler that you want a specific opcode in the generated executable, you&#x27;re merely specifying a particular observable behavior. This is why optimizations such as inlining and TCO are valid. If the compiler can prove that a heap allocation can be turned into a stack allocation, or even removed altogether (e.g. free(malloc(1ULL &lt;&lt; 50))), the fact that these are exposed to the programmer as &quot;functions&quot; he can &quot;call&quot; poses no obstacle.
                                  • 11223338 days ago
                                    Closest to what you say that I can find is 5.1.2.3 §4 of N3096<p><pre><code> In the abstract machine, all expressions are evaluated as specified by the semantics. An actual implementation need not evaluate part of an expression if it can deduce that its value is not used and that no needed side effects are produced (including any caused by calling a function or through volatile access to an object) </code></pre> Problem is, calling external library function has a needed side effect of calling that library function. I do not see language that allows simply not doing that, based on assumed but unknown function behaviour.
                                    • fluoridation37 days ago
                                      The behavior of the standard functions is not unknown, it is at least partially specified. If a user overrides them under the mistaken assumption that a call in source translates in a 1-to-1 correspondence to a call in binary, that&#x27;s their problem.
                                    • uecker38 days ago
                                      You should read &quot;7.1.4 1 Use of library functions&quot;. Also &quot;calling a function&quot; is not a side effect.
                                      • 11223337 days ago
                                        Thanks, I did read it! Things like footnote 236: &quot;This means that an implementation is required to provide an actual function for each library function, even if it also provides a macro for that function&quot;, where macro is shown to use compiler builtin as an example.<p>Again, could you please explain how compiler can decide to remove call to a function in an external dynamically loaded library, that is not known at compile time, simply based on the name of the function (i.e. not because the call is unreachable)? I do not see any such language in the standard.<p>And yes, calling unknown function from a dynamically loaded library totally is a side effect.
                                        • aw162110737 days ago
                                          &gt; Again, could you please explain how compiler can decide to remove call to a function in an external dynamically loaded library, that is not known at compile time, simply based on the name of the function (i.e. not because the call is unreachable)? I do not see any such language in the standard.<p>&gt; And yes, calling unknown function from a dynamically loaded library totally is a side effect.<p>The thing is that malloc&#x2F;free <i>aren&#x27;t</i> &quot;unknown function[s]&quot;. From the C89 standard:<p>&gt; All external identifiers declared in any of the headers are reserved, whether or not the associated header is included.<p>And from the C23 standard:<p>&gt; All identifiers with external linkage in any of the following subclauses (including the future library directions) and errno are always reserved for use as identifiers with external linkage<p>malloc&#x2F;free are defined in &lt;stdlib.h&gt; and so are reserved names, so compilers are able to optimize under the assumption that malloc&#x2F;free will have the semantics dictated by the standard.<p>In fact, the C23 standard explicitly provides an example of this kind of thing:<p>&gt; Because external identifiers and some macro names beginning with an underscore are reserved, implementations can provide special semantics for such names. For example, the identifier _BUILTIN_abs could be used to indicate generation of in-line code for the abs function. Thus, the appropriate header could specify<p><pre><code> #define abs(x) _BUILTIN_abs(x) </code></pre> &gt; for a compiler whose code generator will accept it.
                                        • uecker36 days ago
                                          Only answering the &quot;side effect&quot; part as the rest was answered already.<p>What a side effect is, is explained in &quot;5.1.2.3&quot;. Calling function is only a side effect when the function contains a side effect, such as modifying an object, or a volatile access, or I&#x2F;O.
                                • shakna38 days ago
                                  In C, malloc and free used to often be macros.<p>As they&#x27;re freely replaceable through loading, and designed for that, I would strongly suggest that are among the most magical areas of the C standard.<p>We get a whole section for those in the standard: 7.24.3 Memory management functions<p>Hell, malloc is allowed to return you _less than you asked for_:<p>&gt; The pointer returned if the allocation succeeds is suitably aligned so that it may be assigned to a pointer to any type of object with a fundamental alignment requirement and size less than or equal to the size requested
                                  • 11223338 days ago
                                    I read the text as saying the object size can be less or equal to returned memory size. Anyway, section 7 is library. As you say, replacing through loading is a common thing to do — surely compiler is not free to simply elide external library function at will? This is not C++ after all, it must be sensible
                                    • shakna38 days ago
                                      If the function is equivalent to a no-op, and not explicitly marked as volatile for side-effects, it absolutely can elide it. If there is a side-effect in hardware or wider systems like the OS, then it must be marked as volatile. If the code is just code, then a function call that does effectively nothing, will probably become nothing.<p>That was one of the first optimisations we had, back with Fortran and COBOL. Before C existed - and as B started life as a stripped down Fortran compiler, the history carried through.<p>The K&amp;R book describes the buddy system for malloc, and how its design makes it suitable for compiler optimisations - including ignoring a write to a pointer that does nothing, because the pointer will no longer be valid.
                                      • pklausler38 days ago
                                        Where exactly does K&amp;R specify the buddy system?
                                        • shakna33 days ago
                                          Page 185 - 189.
                                          • pklausler31 days ago
                                            Those pages in K&amp;R define primary expressions through shift expressions.
                                      • 11223338 days ago
                                        You are literally scaring me now. I&#x27;d understand such things being done when statically linking or running JIT, but for &quot;normal&quot; program which function implementation malloc() will link against is not known during compilation. How can compiler go, like, &quot;eh, I&#x27;ll assume free(malloc(x)) is NOP and drop it&quot; and not break most existing code?
                                        • aw162110738 days ago
                                          &gt; but for &quot;normal&quot; program which function implementation malloc() will link against is not known during compilation. How can compiler go, like, &quot;eh, I&#x27;ll assume free(malloc(x)) is NOP and drop it&quot; and not break most existing code?<p>I&#x27;d suspect that eliding suitable malloc&#x2F;free pairs would not break most existing code because most existing code simply does not depend on malloc&#x2F;free doing anything other than and&#x2F;or beyond what the C standard requires.<p>How would you propose that eliding free(malloc(x)) would break &quot;most&quot; existing code, anyways?
                                          • 11223337 days ago
                                            As an example, user kentonv wrote: &quot;I patched the memory allocator used by the Cloudflare Workers runtime to overwrite all memory with a static byte pattern on free&quot;. And compiler would, like, &quot;nah, let&#x27;s leave all that data on stack&quot;.<p>Or somebody would try to plug in mimalloc&#x2F;jemalloc or a debug allocator and wonder what&#x27;s going on.
                                            • fluoridation37 days ago
                                              &gt;As an example, user kentonv wrote: &quot;I patched the memory allocator used by the Cloudflare Workers runtime to overwrite all memory with a static byte pattern on free&quot;. And compiler would, like, &quot;nah, let&#x27;s leave all that data on stack&quot;.<p>Such a program would continue to function as normal; the dirty data would just be left on the stack. If the developer wants to clear that data too, they&#x27;d just have to modify the compiler to overwrite the stack just before (or just after) moving the stack pointer.<p>&gt;Or somebody would try to plug in mimalloc&#x2F;jemalloc or a debug allocator and wonder what&#x27;s going on.<p>Again, that wouldn&#x27;t be broken. They would see that no dynamic allocations were performed during that particular section. Which would be correct.
                                            • aw162110737 days ago
                                              I&#x27;m a bit skeptical either example is representative of &quot;most&quot; existing software. If anything, the mere existence of __builtin_malloc and its default use should hint that most existing software <i>doesn&#x27;t</i> care about malloc&#x2F;free actually being called. That being said...<p>&gt; As an example, user kentonv wrote: &quot;I patched the memory allocator used by the Cloudflare Workers runtime to overwrite all memory with a static byte pattern on free&quot;. And compiler would, like, &quot;nah, let&#x27;s leave all that data on stack&quot;.<p>Strictly speaking, I don&#x27;t think eliding malloc&#x2F;free would &quot;break&quot; those programs because that behavior is there for security if&#x2F;when something else goes wrong, not as part of the software&#x27;s regular intended functionality (or at least I sure hope nothing relies on that behavior for proper functioning!).<p>&gt; Or somebody would try to plug in mimalloc&#x2F;jemalloc [] and wonder what&#x27;s going on.<p>Why would mimalloc&#x2F;jemalloc&#x2F;some other general-purpose allocator care that it doesn&#x27;t have to execute a matching malloc&#x2F;free pair any more than the default allocator?<p>I&#x27;m not sure debug allocators would care either? If you&#x27;re trying to debug mismatched malloc&#x2F;free pairs then the ones the compiler elides are the ones you don&#x27;t care about anyways since those are the ones that can be statically proven to be &quot;self-contained&quot; and&#x2F;or correct. If you&#x27;re gathering statistics then you probably care more about the malloc&#x2F;free calls that <i>do</i> occur (i.e., the ones that can&#x27;t be elided), not those that don&#x27;t.<p>In any case, if you want to use a malloc&#x2F;free implementation that promises more than the C standard does (e.g., special byte pattern on free, statistics&#x2F;debug info tracking, etc.) there&#x27;s always -fno-builtin-malloc (or memset_explicit if you&#x27;re lucky enough to be using C23). Of course, the tradeoff is that you give up some potential performance.
                                              • 11223337 days ago
                                                Thank you for putting it in a much more correct and understandable language than I could. That is exactly what I am talking about: if you call __builtin_malloc (e.g. via macro definition in the libc header), compiler is free to do whatever it wants. However, calling &quot;malloc&quot; library function should call &quot;malloc&quot; library function, and anything else is unacceptable and a bug. There should be no case where compiler could assume anything about a function it does not see based simply on it&#x27;s name. Neither malloc nor strlen.
                                                • aw162110737 days ago
                                                  &gt; That is exactly what I am talking about: if you call __builtin_malloc (e.g. via macro definition in the libc header), compiler is free to do whatever it wants. However, calling &quot;malloc&quot; library function should call &quot;malloc&quot; library function, and anything else is unacceptable and a bug.<p>I think that&#x27;s an overly narrow reading of the footnote. I don&#x27;t see an obvious reason why &quot;such names&quot; in the footnote should only cover &quot;some macro names beginning with an underscore&quot; and not also &quot;external identifiers&quot;. And if implementations are allowed to define special semantics for &quot;external identifiers&quot;, then... well, that&#x27;s exactly what they did!<p>In addition, there&#x27;s still the as-if rule. The semantics of malloc&#x2F;free are defined by the C standard; if the compiler can deduce that there is no observable difference between a version of the program that calls those and a version that does not, why does it matter that the call is emitted? A function call in and of itself is not a side effect, and since the C standard dictates what malloc&#x2F;free do the compiler knows their possible side effects.<p>Furthermore, the addition of memset_explicit and its footnote (&quot;The intention is that the memory store is always performed (i.e. never elided), regardless of optimizations. This is in contrast to calls to the memset function (7.26.6.1)&quot;) implies that eliding calls is in fact acceptable behavior when optimizations are enabled. If eliding calls were not permissible when optimizing then what&#x27;s the point of memset_explicit?<p>&gt; There should be no case where compiler could assume anything about a function it does not see based simply on it&#x27;s name.<p>Again, <i>external identifiers defined by the C standard are reserved</i>. Reserved external identifiers aren&#x27;t just for show. From the C89 standard:<p>&gt; If the program defines an external identifier with the same name as a reserved external identifier, even in a semantically equivalent form, the behavior is undefined.<p>And from C23:<p>&gt; If the program declares or defines an identifier in a context in which it is reserved (other than as allowed by 7.1.4), the behavior is undefined.<p>This means that yes, under modern compilers&#x27; interpretation of UB compilers <i>can</i> assume things about functions based on their names because modern compilers generally optimize assuming UB does not happen. The compiler does not need to see the function&#x27;s implementation because it <i>is</i> the function&#x27;s implementation as far as it is concerned.
                                                  • 11223337 days ago
                                                    Ah yes, N2625 &quot;What we think we reserve&quot;. Basically any C program containing variable or function &quot;top&quot;, &quot;END&quot;, &quot;strict&quot;, &quot;member&quot; and so on is non-conforming and subject to undefined behaviour, so they define &quot;potentially reserved&quot; identifiers and as usual compiler vendors go and do the sane right thing.
                                                    • aw162110737 days ago
                                                      That paper isn&#x27;t relevant here. From the paper (emphasis added):<p>&gt; 7.1.3 Reserved Identifiers<p>&gt; [snip]<p>&gt; Macro names and identifiers with external linkage that are specified in the C standard library clauses.<p>&gt; This proposal <i>does not propose any changes to these reserved identifiers</i>.<p>Furthermore, that paper doesn&#x27;t make the use of reserved external identifiers not UB, so there&#x27;s no change there either.
        • maxlybbert40 days ago
          Newer versions of C++ (and C, apparently) have functions so that the cast isn&#x27;t necessary ( <a href="https:&#x2F;&#x2F;en.cppreference.com&#x2F;w&#x2F;c&#x2F;string&#x2F;byte&#x2F;memset.html" rel="nofollow">https:&#x2F;&#x2F;en.cppreference.com&#x2F;w&#x2F;c&#x2F;string&#x2F;byte&#x2F;memset.html</a> ).
    • rectang39 days ago
      Zeroing memory should absolutely be the default behavior for any generic allocator in 2025.<p>If you need better performance, write your own allocator optimized for your specific use case — it&#x27;s not that hard.<p>Besides, you if you don&#x27;t need to clear old allocations, there are likely other optimizations you&#x27;ll be able to find which would never fly in a system allocator.
    • tombert40 days ago
      You know, I never even considered doing that but it makes sense; whatever overhead that&#x27;s incurred by doing that static byte pattern is still almost certainly minuscule compared to the overhead of something like a garbage collector.
      • ddtaylor40 days ago
        IMO the tradeoff that is important here is a few microseconds of time sanitizing the memory saves the millions of dollars of headache when memory unsafe languages fail (which happens regularly)
        • tombert40 days ago
          I agree. I almost feel like this should be like a flag in `free`. Like if you pass in 1 or something as a second argument (or maybe a `free_safe` function or something), it will automatically `memset` whatever it&#x27;s freeing with 0&#x27;s, and then do the normal freeing.
          • miki12321139 days ago
            Alternatively, just make free do that by default, adding a fast_and_furious_free which doesn&#x27;t do it, for the few hotspots where that tiny bit of performance is actually needed.
            • sitkack39 days ago
              The default case should be the safe correct one, even if it “breaks” backward compatibility. Without it, we will forever be saddled with the design mistakes of the past.
          • yawaramin40 days ago
            <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=46417221">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=46417221</a>
      • anonymars39 days ago
        Non-deterministic latency is a drawback, but garbage collection is not inherently slower than manual memory management&#x2F;reference counting&#x2F;etc. Depending on the usage pattern it can be faster. It&#x27;s a set of trade-offs
    • dfc39 days ago
      Is this the same as enabling `init_on_free=1` in the kernel?
  • plorkyeran40 days ago
    The author seems to be unaware that Mongo internally develops in a private repo and commits are published later to the public one with <a href="https:&#x2F;&#x2F;github.com&#x2F;google&#x2F;copybara" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;google&#x2F;copybara</a>. All of the confusion around dates is due to this.
    • enether39 days ago
      I was definitely unaware. I suspected something like this may be up when I talked about the zero-review of the apparent PR &quot;I’m not aware of Mongo’s public review practices&quot;. This is great to know though. Updating the piece now to mention this and explain the date discrepancy
  • computerfan49440 days ago
    The author of this post is incorrect about the timeline. Our Atlas clusters were upgraded days before the CVE was announced.
  • maxrmk40 days ago
    How often are mongo instances exposed to the internet? I&#x27;m more of an SQL person and for those I know it&#x27;s pretty uncommon, but does happen.
    • petcat40 days ago
      From my experience, Mongo DB&#x27;s entire raison d&#x27;etre is &quot;laziness&quot;.<p>* Don&#x27;t worry about a schema.<p>* Don&#x27;t worry about persistence or durability.<p>* Don&#x27;t worry about reads or writes.<p>* Don&#x27;t worry about connectivity.<p>This is basically the entire philosophy, so it&#x27;s not surprising at all that users would also not worry about basic security.
      • winrid40 days ago
        Although interestingly, for all the mongo deployments I managed, the first time I saw a cluster publicly exposed without SSL was postgres :)
      • senderista40 days ago
        To the extent that any of this was ever true, it hasn’t been true for at least a decade. After the WiredTiger acquisition they really got their engineering shit together. You can argue it was several years too late but it did happen.
        • cyberpunk39 days ago
          I got heavily burned pre-wiredtiger and swore to never use it again. Started a new job which uses it and it’s been… Painless, stable and fast with excellent support and good libraries. They did turn it around for sure.
      • aragilar40 days ago
        Not only that, but authentication is much harder than it needs to be to set up (and is off by default).
      • morshu900140 days ago
        I&#x27;m sure there are publicly exposed MySQLs too
        • zX41ZdbW39 days ago
          There are many more exposed MySQLs than MongoDBs:<p><a href="https:&#x2F;&#x2F;www.shodan.io&#x2F;search?query=mongodb" rel="nofollow">https:&#x2F;&#x2F;www.shodan.io&#x2F;search?query=mongodb</a> <a href="https:&#x2F;&#x2F;www.shodan.io&#x2F;search?query=mysql" rel="nofollow">https:&#x2F;&#x2F;www.shodan.io&#x2F;search?query=mysql</a> <a href="https:&#x2F;&#x2F;www.shodan.io&#x2F;search?query=postgresql" rel="nofollow">https:&#x2F;&#x2F;www.shodan.io&#x2F;search?query=postgresql</a><p>But this must be proportional to the overall popularity.
      • Thaxll40 days ago
        Most of your points are wrong. Maybe only 1- is valid&#x27;ish.
      • ddtaylor40 days ago
        Ultimate webscale!
    • hahahacorn40 days ago
      A highly cited reason for using mongo is that people would rather not figure out a schema. (N=3&#x2F;3 for “serious” orgs I know using mongo).<p>That sort of inclination to push off doing the right thing now to save yourself a headache down the line probably overlaps with “let’s just make the db publicly exposed” instead of doing the work of setting up an internal network to save yourself a headache down the line.
      • matwood39 days ago
        &gt; A highly cited reason for using mongo is that people would rather not figure out a schema.<p>Which is such a cop out, because there is <i>always</i> a schema. The only questions are whether it is designed, documented, and where it&#x27;s implemented. Mongo requires some very explicit schema decisions, otherwise performance will quickly degrade.
        • xnorswap39 days ago
          Fowler describes it as Implicit vs Explicit schema, which feels right.<p>Kleppmann chooses &quot;schema-on-read&quot; vs &quot;schema-on-write&quot; for the same concept, which I find harder to grasp mentally, but describes when schema validation need occur.
      • TZubiri40 days ago
        I would have hoped that there would be no important data in mongoDB.<p>But now we can at least be rest assured that the important data in mongoDB is just very hard to read with the lack of schemas.<p>Probably all of that nasty &quot;schema&quot; work and tech debt will finally be done by hackers trying to make use of that information.
        • bostik39 days ago
          There is a surprising amount of important data in various Mongo instances around the world. Particularly within high finance, with multi-TB setups sprouting up here and there.<p>I suspect that this is in part due to historical inertia and exposure to SecDB designs.[0] Financial instruments can be <i>hideously complex</i> and they certainly are ever-evolving, so I can imagine a fixed schema for essentially constantly shifting time series universe would be challenging. When financial institutions began to adopt the SecDB model, MongoDB was available as a high-volume, &quot;schemaless&quot; KV store, with a reasonably good scaling story.<p>Combine that with the relatively incestuous nature of finance (they tend to poach and hire from within their own ranks), the average tenure of an engineer in one organisation being less than 4 years and you have an osmotic process of spreading &quot;this at least works in this type of environment&quot; knowledge. Add the naturally risk-averse nature of finance[ß] and you can see how one successful early adoption will quickly proliferate across the industry.<p>0: This was discussed at HN back in the day too: <a href="https:&#x2F;&#x2F;calpaterson.com&#x2F;bank-python.html" rel="nofollow">https:&#x2F;&#x2F;calpaterson.com&#x2F;bank-python.html</a><p>ß: For an industry that loves to take financial risks - with other people&#x27;s money of course, they&#x27;re not stupid - the players in high finance are remarkably risk-averse when it comes to technology choices. Experimentation with something new and unknown carries a potentially unbounded downside with limited, slowly emerging upside.
        • saghm40 days ago
          I&#x27;d argue that there&#x27;s a schema; it&#x27;s just defined dynamically by the queries themselves. Given how much of the industry seems fine with dynamic typing in languages, it&#x27;s always been weird to me how diehard people seem to be about this with databases. There have been plenty of legitimate reasons to be skeptical of mongodb over the years (especially in the early days), but this one really isn&#x27;t any more of a big deal than using Python or JavaScript.
          • morshu900140 days ago
            Yes there&#x27;s a schema, but it&#x27;s hard to maintain. You end up with 200 separate code locations rechecking that the data is in the expected shape. I&#x27;ve had to fix too many such messes at work after a project grinded to a halt. Ironically some people will do schemaless but use a statically typed lang for regular backend code, which doesn&#x27;t buy you much. I&#x27;d totally do dynamic there. But DB schema is so little effort for the strong foundation it sets for your code.<p>Sometimes it comes from a misconception that your schema should never have to change as features are added, and so you need to cover all cases with 1-2 omni tables. Often named &quot;node&quot; and &quot;edge.&quot;
            • saghm39 days ago
              &gt; Ironically some people will do schemaless but use a statically typed lang for regular backend code, which doesn&#x27;t buy you much. I&#x27;d totally do dynamic there.<p>I honestly feel like the opposite, at least if you&#x27;re the only consumer of the data. I&#x27;d never really go out of my way to use a dynamically typed language, and at that point, I&#x27;m already going to be having to do something to get the data into my own language&#x27;s types, and at that point, it doesn&#x27;t really make a huge difference to me what format it used to be in. When there are a variety of clients being used though, this logic might not apply though.
              • morshu900135 days ago
                If you&#x27;re only consuming, yes. It might as well be a totally separate service. If it&#x27;s your database that you read&#x2F;write on, it&#x27;s closely tied to your code.
            • cyberpunk39 days ago
              We just sit a data persistence service infront of mongo and so we can enforce some controls for everything there if we need them, but quite often we don’t.<p>It’s probably better to check what you’re working on than blindly assuming this thing you’ve gotten from somewhere is the right shape anyway.
              • morshu900135 days ago
                The &quot;DAO&quot; way like this is usually how it goes. It tends to become bloated. Best case, you&#x27;re reimplementing what the schema would&#x27;ve done for you anyway.
            • matwood39 days ago
              The adage I always tell people is that in any successful system, the data will far outlive the code. People throw away front ends and middle layers all the time. This becomes so much harder to do if the schema is defined across a sprawling middle layer like you describe.
          • jeltz39 days ago
            As someone who has done a lot of Ruby coding I would say using a statically typed database is almost a must when using a dynamically type language. The database enforces the data model and the Ruby code was mostly just glue on top of that data model.
            • saghm39 days ago
              That&#x27;s fair, I could see an argument for &quot;either the schema or the language needs to enforce schema&quot;. It&#x27;s not obvious to me that one of the two models of &quot;only one of them is&quot; deserves to much more criticism than the other though.
          • TZubiri39 days ago
            What&#x27;s weird to me is when dynamic typers don&#x27;t acknowledge the tradeoff of quality vs upfront work.<p>I never said mongodb was wrong in that post, I just said it accumulated tech debt.<p>Let&#x27;s stop feeling attacked over the negatives of tradeoffs
            • saghm37 days ago
              It&#x27;s possible you didn&#x27;t intend it, but your parent comment definitely came off as snarky, so I don&#x27;t think you should be surprised that people responded in kind. You&#x27;re honestly doing it again with the &quot;let&#x27;s stop feeling attacked&quot; bit; whether you mean it or not, your phrasing comes across as pretty patronizing, and overall combined with the apparent dislike of people disagreeing with you after the snark it comes across as passive-aggressive. In general it&#x27;s not going to go over well if you dish out criticism but can&#x27;t take it.<p>In any case, you quite literally said there was a &quot;lack of schemas&quot;, and I disagreed with that characterization. I certainly didn&#x27;t feel attacked by it; I just didn&#x27;t think it was the most accurate way to view things from a technical perspective.
        • bigbuppo39 days ago
          Whatever horrors there are with mongo, it&#x27;s still better than the shitshow that is Zope&#x27;s ZODB.
    • wood_spirit40 days ago
      The article links to a shodan scan reporting 213K exposed instances <a href="https:&#x2F;&#x2F;www.shodan.io&#x2F;search?query=Product%3A%22MongoDB%22" rel="nofollow">https:&#x2F;&#x2F;www.shodan.io&#x2F;search?query=Product%3A%22MongoDB%22</a>
    • ddtaylor40 days ago
      It could be because when you leave an SQL server exposed it often turns into much worse things. For example, without additional configuration, PostgreSQL will default into a configuration that can own the entire host machine. There is probably some obscure feature that allows system process management, uploading a shell script or something else that isn&#x27;t disabled by default.<p>The end result is &quot;everyone&quot; kind of knows that if you put a PostgreSQL instance up publicly facing without a password or with a weak&#x2F;default password, it will be popped in minutes and you&#x27;ll find out about it because the attackers are lazy and just running crypto-mine malware, etc.
    • acheong0840 days ago
      My university has one exposed to the internet, and it&#x27;s still not patched. Everyone is on holiday and I have no idea who to contact.
      • heavyset_go40 days ago
        No one, if you aren&#x27;t in the administration&#x27;s good graces and something shitty happens unrelated to you, you&#x27;ve put a target on your back to be suspect #1.
      • bschmidt10797940 days ago
        [dead]
    • ok12345640 days ago
      For a long time, the default install had it binding to all interfaces and with authentication disabled.
    • notepad0x9040 days ago
      often. lots of data leaks happened because of this. people spin it up in a cloud vm and forget it has a public ip all the time.
    • bschmidt10797940 days ago
      [dead]
      • ch202640 days ago
        Because nobody uses mongo for the reasons you listed. They use redis, dynamo, scylla or any number of enriched KV stores.<p>Mongo has spent its entire existence pretending to be a SQL database by poorly reinventing everything you get for free in postgres or mysql or cockroach.
        • yearolinuxdsktp39 days ago
          False. Mongo never pretended to be a SQL database. But some dimwits insisted on using it for transactions, for whatever reason, and so it got transactional support, way later in life, and in non-sharded clusters in the initial release. People that know what they are doing have been using MongoDB for reliable horizontally-scalable document storage basically since 3.4. With proper complex indexing.<p>Scylla! Yes, it will store and fetch your simple data very quickly with very good operational characteristics. Not so good for complex querying and indexing.
        • bschmidt10797939 days ago
          [dead]
      • maxrmk39 days ago
        Yeah fair, I was being a bit lazy here when writing my comment. I&#x27;ve used nosql professionally quite a bit, but always set up by others. When working on personal projects I reach for SQL first because I can throw something together and don&#x27;t need ideal performance. You&#x27;re absolutely right that they both have their place.<p>That being said the question was genuine - because I don&#x27;t keep up with the ecosystem, I don&#x27;t know it&#x27;s ever valid practice to have a nosql db exposed to the internet.
      • Capricorn248140 days ago
        What they wrote was pretty benign. They just asked how common it is for Mongo to be exposed. You seem to have taken that as a completely different statement
  • esprehn39 days ago
    In something like a database zeroing or poisoning on free is probably a good idea. (These days probably all allocators should do it by default.)<p>Allocators are an interesting place to focus on for security. Chris did amazing work there for Blink that eventually rolled out to all of Chromium. The docs are a fun read.<p><a href="https:&#x2F;&#x2F;blog.chromium.org&#x2F;2021&#x2F;04&#x2F;efficient-and-safe-allocations-everywhere.html" rel="nofollow">https:&#x2F;&#x2F;blog.chromium.org&#x2F;2021&#x2F;04&#x2F;efficient-and-safe-allocat...</a><p><a href="https:&#x2F;&#x2F;chromium.googlesource.com&#x2F;chromium&#x2F;src&#x2F;+&#x2F;master&#x2F;base&#x2F;allocator&#x2F;partition_allocator&#x2F;PartitionAlloc.md" rel="nofollow">https:&#x2F;&#x2F;chromium.googlesource.com&#x2F;chromium&#x2F;src&#x2F;+&#x2F;master&#x2F;base...</a>
  • whynotmaybe40 days ago
    I&#x27;m still thinking about the hypothetical optimism brought by OWASP top 10 hoping that major flaws will be solved and that buffer overflow has been there since the beginning... in 2003.
    • thrwaway5540 days ago
      I mean giving everyone footguns and you&#x27;ll find that is unavoidable forever. Thoughts and prayers to the Mongo devs until we migrate to a language that prevents this error.
  • exabrial40 days ago
    Why is anyone using mongo for literally anything
    • nine_k40 days ago
      Easy replication. I suppose it&#x27;s faster than Postgres&#x27;s JSONB, too.<p>I would rather not use it, but I see that there are legitimate cases where MongoDB or DynamoDB is a technically appropriate choice.
    • mickael-kerjean40 days ago
      because it is &quot;web scale&quot;<p>ref: <a href="https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=b2F-DItXtZs" rel="nofollow">https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=b2F-DItXtZs</a>
      • DonHopkins39 days ago
        Whenever anyone writes about mongodb or redis I hear it in that voice.
    • gethly39 days ago
      Right? When they came out, it was all about NoSQL, which then turned out only mean key-value database, whom are plentiful.
    • Aldipower40 days ago
      This is a nasty ad repositorium datorum argumentation which I cannot tolerate.
  • netsharc40 days ago
    &gt; On Dec 24th, MongoDB reported they have no evidence of anybody exploiting the CVE<p>Absence of evidence is not evidence of absence...
    • forrestthewoods40 days ago
      What would you prefer them to say?
      • perching_aix40 days ago
        Evidence of no exploitations? It&#x27;s usually hard to prove a negative, except when you have all the logs at your fingertips you can sift through. Unless they don&#x27;t, of course. In which case the point stands: they don&#x27;t actually know at this point in time, if they can even know about it at all.<p>Specifically, it looks like the exflitration primitive relies on errors being emitted, and those errors are what leak the data. They&#x27;re also rather characteristic. One wouldn&#x27;t reasonably expect MongoDB to hold onto all raw traffic data flowing in and out, but would <i>absolutely</i> expect them to have the error logs, at least for some time back.
        • saghm40 days ago
          I feel like that&#x27;s an issue not with what they said, but what they did. It would be better for them to have checked this quickly, but it would have been worse for them to have they did when they hadn&#x27;t. What you&#x27;re saying isn&#x27;t wrong, but it&#x27;s not really an answer to the question you&#x27;re replying to.
        • forrestthewoods40 days ago
          “No evidence of exploitation” is a pretty bog standard report I think? Made on Christmas Eve no less.<p>Do other CVE reports come with more strong statements? I’m not sure they do. But maybe you can provide some counter examples that meet your bar.
          • dwattttt39 days ago
            &gt; &quot;No evidence of exploitation” is a pretty bog standard report<p>It is standard, yes. The problem with it as a statement is that it&#x27;s true even if you&#x27;ve collected exactly zero evidence. I can say I don&#x27;t have evidence of anyone being exploited, and it&#x27;s definitely true.
          • perching_aix40 days ago
            It&#x27;s not really my bar, I just explored this on behalf of the person you were replying to because I found it mildly interesting.<p>It is also a pretty standard response indeed. But now that it was highlighted, maybe it does deserve some scrutiny? Or is saying silly, possibly misleading things okay if that&#x27;s what everyone has always been doing?
  • ChrisArchitect40 days ago
    Related:<p><i>MongoBleed</i><p><a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=46394620">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=46394620</a>
  • petesergeant40 days ago
    &gt; In C&#x2F;C++, this doesn’t happen. When you allocate memory via `malloc()`, you get whatever was previously there.<p>What would break if the compiler zero&#x27;d it first? Do programs rely on malloc() giving them the data that was there before?
    • pelorat39 days ago
      That&#x27;s what calloc() is for
    • mdavid62639 days ago
      It takes time to zero out memory.
  • vivzkestrel40 days ago
    is it true that ubisoft got hacked and 900GB of data from their database was leaked due to mongobleed, i am seeing a lot of posts on social media under the #ubisoft tags today. can someone on HN confirm?
    • christophilus40 days ago
      I read that hack was made possible by Ubisoft’s support staff taking bribes.
      • Maxious40 days ago
        Details are still emerging, update in the last hour was that at least 5 different hacking groups were in ubisoft&#x27;s systems and yeah some might have got their via bribes rather than mongodb <a href="https:&#x2F;&#x2F;x.com&#x2F;vxunderground&#x2F;status&#x2F;2005483271065387461" rel="nofollow">https:&#x2F;&#x2F;x.com&#x2F;vxunderground&#x2F;status&#x2F;2005483271065387461</a>
        • sitkack39 days ago
          I’ll give you $1000 to run Mongo.
    • bschmidt10797940 days ago
      [dead]
  • dwheeler40 days ago
    This has many similarities to the Heartbleed vulnerability: it involves trusting lengths from an attacker, leading to unauthorized revelation of data.
  • ldng39 days ago
    MongoDB has always sucked... But it&#x27;s webscale (sic)<p>Do yourself a favour, use ToroDB instead (or even straight PostgreSQL&#x27;s JSONB).
  • reassess_blind40 days ago
    Have all Atlas clusters been auto-updated with a fix?
    • enether39 days ago
      yes. apparently before Dec 19 too
  • fwip40 days ago
    &quot;MongoBleed Explained by an LLM&quot;
    • tuetuopay39 days ago
      If it is, it&#x27;s less fluffy and empty than most of LLM prose we&#x27;re usually fed. It&#x27;s well explained and has enough details to not be overwhelming.<p>Honestly, aside from the &quot;&lt;emoji&gt; impact&quot; section that really has an LLM smell (but remember that some people legit do this since it&#x27;s in the llm training corpus), this more feels like LLM assisted (translated? reworded? grammar-checked?) that pure &quot;explain this&quot; prompt.
      • enether39 days ago
        I didn&#x27;t use AI in writing the post.<p>I did some research with it, and used it to help create the ASCII art a bit. That&#x27;s about it.<p>I was afraid that adding the emoji would trigger someone to think it&#x27;s AI.<p>In any case, nowadays I basically always get at least one comment calling me an AI on a post that&#x27;s relatively popular. I assume it&#x27;s more a sign of the times than the writing...
        • tuetuopay39 days ago
          Thank you for the clarification! I&#x27;m sorry for engaging in the LLM hunt, I don&#x27;t usually do. Please keep writing, this was a really good breakdown!<p>In hindsight, I would not even have thought about it if not for the comment I replied to. LLM prose fail to make me read whole paragraphs and I find myself skipping roughly the second half of every paragraph, which was definitely not the case for your article. I did somewhat skip at the emoji heading, not because of LLMs, but because of a saturation of emojis in some contexts that don&#x27;t really need them.<p>I should have written &quot;this could be LLM assisted&quot; instead of &quot;this more feels like LLM assisted&quot;, but well words.<p>Again, sorry, don&#x27;t get discouraged by the LLM witch hunt.
        • macintux39 days ago
          I’m about ready to start flagging every comment that complains about the source material being LLM-generated. It’s tiresome, pointless, and adds absolutely nothing useful to the discussion.<p>If the material is wrong, explain why. Otherwise, shut up.
    • beembeem39 days ago
      Though the source article was human written, the public exploit was developed with an LLM.<p><a href="https:&#x2F;&#x2F;x.com&#x2F;dez_&#x2F;status&#x2F;2004933531450179931" rel="nofollow">https:&#x2F;&#x2F;x.com&#x2F;dez_&#x2F;status&#x2F;2004933531450179931</a>
  • bschmidt10797940 days ago
    [dead]
    • jeltz39 days ago
      Nah, this time it was just you.
  • hindustanuday39 days ago
    [dead]
  • hindustanuday39 days ago
    [dead]