{"id":293,"date":"2025-12-22T14:33:07","date_gmt":"2025-12-22T14:33:07","guid":{"rendered":"https:\/\/haco.club\/?p=293"},"modified":"2025-12-22T14:33:07","modified_gmt":"2025-12-22T14:33:07","slug":"the-difference-of-overflow-and-underflow","status":"publish","type":"post","link":"https:\/\/haco.club\/?p=293","title":{"rendered":"The difference of overflow and underflow"},"content":{"rendered":"\n<p>In computer science\u2014and specifically in fuzzing and exploitation\u2014the terms <strong>Overflow<\/strong> and <strong>Underflow<\/strong> mean different things depending on whether you are talking about <strong>Numbers (Arithmetic)<\/strong> or <strong>Memory (Buffers)<\/strong>.<\/p>\n\n\n\n<p>Here is the breakdown of the differences.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">1. Arithmetic (Integer) Context<\/h3>\n\n\n\n<p>This refers to the value of a number going beyond what the variable type can hold.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Integer Overflow (Too Big)<\/h4>\n\n\n\n<p>Occurs when you try to store a value larger than the maximum limit. The value &#8220;wraps around&#8221; to the minimum.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Analogy:<\/strong> A car odometer at <code>999,999<\/code> rolling over to <code>000,000<\/code>.<\/li>\n\n\n\n<li><strong>Example (8-bit unsigned):<\/strong> The max value is 255.\n<ul class=\"wp-block-list\">\n<li><code>255 + 1 = 0<\/code><\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Security Risk:<\/strong> If you calculate <code>size = num_elements * 10<\/code> and it overflows to a small number (e.g., 4), you might allocate only 4 bytes of memory but try to copy huge amounts of data into it, causing a heap overflow.<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">Integer Underflow (Too Small)<\/h4>\n\n\n\n<p>Occurs when you try to go below the minimum limit. The value &#8220;wraps around&#8221; to the maximum.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Analogy:<\/strong> Rolling a car odometer backward from <code>000,000<\/code> to <code>999,999<\/code>.<\/li>\n\n\n\n<li><strong>Example (8-bit unsigned):<\/strong> The min value is 0.\n<ul class=\"wp-block-list\">\n<li><code>0 - 1 = 255<\/code><\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Security Risk:<\/strong> If you have a check like <code>if (packet_len &lt; header_len) return;<\/code>, and you do <code>payload_size = packet_len - header_len<\/code>, an underflow could make <code>payload_size<\/code> a massive number (like 4 billion), causing a massive copy loop.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">2. Memory \/ Buffer Context<\/h3>\n\n\n\n<p>This refers to accessing memory outside the bounds of an array or buffer. This is the most common target for AFL\/fuzzing.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Buffer Overflow (Going &#8220;Forward&#8221;)<\/h4>\n\n\n\n<p>Writing data <strong>past the end<\/strong> of the allocated buffer (towards higher memory addresses).<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Direction:<\/strong> Low Address $\\to$ High Address.<\/li>\n\n\n\n<li><strong>Code:<\/strong> <code>buf[10] = 'A'<\/code> (when size is 10).<\/li>\n\n\n\n<li><strong>Impact on Stack:<\/strong> Since the Stack grows <strong>down<\/strong> (High to Low), but buffers index <strong>up<\/strong> (Low to High), a buffer overflow overwrites data &#8220;above&#8221; it in the stack frame.\n<ul class=\"wp-block-list\">\n<li><strong>Main Thread:<\/strong> Overwrites the Return Address (saved LR).<\/li>\n\n\n\n<li><strong>Secondary Thread:<\/strong> Writes towards the <strong>TCB<\/strong> (Thread Control Block) and <code>stack_guard<\/code>.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">Buffer Underflow (Going &#8220;Backward&#8221;)<\/h4>\n\n\n\n<p>Writing data <strong>before the start<\/strong> of the allocated buffer (towards lower memory addresses).<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Direction:<\/strong> High Address $\\to$ Low Address.<\/li>\n\n\n\n<li><strong>Code:<\/strong> <code>buf[-1] = 'A'<\/code>.<\/li>\n\n\n\n<li><strong>Impact on Stack:<\/strong> This writes deeper into the stack (into unused space or the next function&#8217;s frame), which is usually less immediately dangerous than an overflow, but can still cause logic bugs.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">3. The &#8220;Stack&#8221; Special Case<\/h3>\n\n\n\n<p>There are two specific terms related to the Stack structure itself (Push\/Pop).<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Stack Overflow:<\/strong> The stack grows so large (usually due to infinite recursion) that it hits the limit of the OS-allocated memory region (the &#8220;Guard Page&#8221;). This causes a <code>SEGFAULT<\/code>.<\/li>\n\n\n\n<li><strong>Stack Underflow:<\/strong> The program tries to <code>POP<\/code> items off the stack when the stack is empty. This usually only happens in handwritten assembly or ROP (Return Oriented Programming) chains, not in normal C code.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Summary Table<\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th class=\"has-text-align-left\" data-align=\"left\">Context<\/th><th class=\"has-text-align-left\" data-align=\"left\">Term<\/th><th class=\"has-text-align-left\" data-align=\"left\">What happens?<\/th><th class=\"has-text-align-left\" data-align=\"left\">Direction \/ Result<\/th><\/tr><\/thead><tbody><tr><td class=\"has-text-align-left\" data-align=\"left\"><strong>Arithmetic<\/strong><\/td><td class=\"has-text-align-left\" data-align=\"left\"><strong>Overflow<\/strong><\/td><td class=\"has-text-align-left\" data-align=\"left\">Value &gt; Max<\/td><td class=\"has-text-align-left\" data-align=\"left\">Wraps to Min (0)<\/td><\/tr><tr><td class=\"has-text-align-left\" data-align=\"left\"><strong>Arithmetic<\/strong><\/td><td class=\"has-text-align-left\" data-align=\"left\"><strong>Underflow<\/strong><\/td><td class=\"has-text-align-left\" data-align=\"left\">Value &lt; Min<\/td><td class=\"has-text-align-left\" data-align=\"left\">Wraps to Max (Huge number)<\/td><\/tr><tr><td class=\"has-text-align-left\" data-align=\"left\"><strong>Memory<\/strong><\/td><td class=\"has-text-align-left\" data-align=\"left\"><strong>Overflow<\/strong><\/td><td class=\"has-text-align-left\" data-align=\"left\">Index &gt; Size<\/td><td class=\"has-text-align-left\" data-align=\"left\">Writes to <strong>Higher<\/strong> Address<\/td><\/tr><tr><td class=\"has-text-align-left\" data-align=\"left\"><strong>Memory<\/strong><\/td><td class=\"has-text-align-left\" data-align=\"left\"><strong>Underflow<\/strong><\/td><td class=\"has-text-align-left\" data-align=\"left\">Index &lt; 0<\/td><td class=\"has-text-align-left\" data-align=\"left\">Writes to <strong>Lower<\/strong> Address<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Relating to your previous question (The TCB)<\/h3>\n\n\n\n<p>In the context of the <strong>Secondary Thread<\/strong> layout I described previously:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Buffer Overflow:<\/strong> If you have a buffer on the stack and you write past the end (<code>buf[1000]<\/code>), you write towards <strong>Higher Addresses<\/strong>.\n<ul class=\"wp-block-list\">\n<li>Because the TCB is located at the <strong>top<\/strong> (High Address) of the thread&#8217;s memory block, a massive <strong>Buffer Overflow<\/strong> on the stack allows you to corrupt the <strong>TCB<\/strong>, <code>stack_guard<\/code>, or TLS pointers.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Buffer Underflow:<\/strong> If you write to <code>buf[-1]<\/code>, you write towards <strong>Lower Addresses<\/strong>.\n<ul class=\"wp-block-list\">\n<li>This moves <em>away<\/em> from the TCB and <em>towards<\/em> the Stack Guard Page at the bottom.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>In computer science\u2014and specifically in fuzzing and exploitation\u2014the terms Overflow [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[42],"tags":[5],"class_list":["post-293","post","type-post","status-publish","format-standard","hentry","category-knowledge-base","tag-security"],"_links":{"self":[{"href":"https:\/\/haco.club\/index.php?rest_route=\/wp\/v2\/posts\/293","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/haco.club\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/haco.club\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/haco.club\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/haco.club\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=293"}],"version-history":[{"count":1,"href":"https:\/\/haco.club\/index.php?rest_route=\/wp\/v2\/posts\/293\/revisions"}],"predecessor-version":[{"id":294,"href":"https:\/\/haco.club\/index.php?rest_route=\/wp\/v2\/posts\/293\/revisions\/294"}],"wp:attachment":[{"href":"https:\/\/haco.club\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=293"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/haco.club\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=293"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/haco.club\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=293"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}