Re: Memory protection between compilation units?

Liste des GroupesRevenir à cl c  
Sujet : Re: Memory protection between compilation units?
De : tr.17687 (at) *nospam* z991.linuxsc.com (Tim Rentsch)
Groupes : comp.lang.c
Date : 12. Jun 2025, 14:05:09
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <86wm9hp0u2.fsf@linuxsc.com>
References : 1
User-Agent : Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Mateusz Viste <mateusz@x.invalid> writes:

This might not be a strictly C question, but it definitely concerns all
C programmers.
>
Earlier today, I fixed an out-of-bounds write bug.  An obvious issue:
>
  static int *socks[0xffff];
>
  void update_my_socks(int *sock, int val) {
    socks[val & 0xffff] = sock;
  }
>
While the presented issue is common knowledge for anyone familiar with
C, *locating* the bug was challenging.  The program did not crash at the
moment of the out-of-bounds write but much later - somewhere entirely
different, in a different object file that maintained a static pointer
for tracking a position in a linked list.  To my surprise, the pointer
was randomly reset to NULL about once a week, causing a segfault.
Tracing this back to an unrelated out-of-bounds write elsewhere in the
code was tedious, to say the least.
>
This raises a question:  how can such corruptions be detected sooner?
Protected mode prevents interference between programs but doesn?t
safeguard a program from corrupting itself.  Is there a way to enforce
memory protection between module files of the same program?  After all,
static objects shouldn't be accessible outside their compilation unit.
>
How would you approach this?

The code in question shows several classic error patterns.  In no
particular order:

  * buffer overflow
  * off-by-one error
  * hard-coded constants (rather than symbolic)
  * bitwise operator with signed operand
  * using & to effect what is really a modulo operation
  * two of the above combine to impose a constraint on a
    hard-coded value, and the constraint is never checked

Of course some of these, notably buffer overflow, are hard to find.
But some of them are easy.  The hard-coded constants stand out like a
neon sign, especially because one is duplicated.  Check for any
constant written in open code above the value of, say, 10.  Once the
offending example is found, it can be rewritten, as for example

   static int *socks[0xffff];
  
   void update_my_socks(int *sock, int val) {
      const unsigned N = sizeof socks / sizeof socks[0];
      socks[val % N] = sock;
   }

This revision doesn't fix the program but it does eliminate the bug.
(Presumably fixing the program will happen later.)  Of course the
code should be further revised so that the temptation to use the
hard-coded value elsewhere is reduced, but this revision at least is
a step in the right direction.

Also, whenever a cockroach is seen, you can be sure there are other
cockroaches around.  Each of the types of errors evidenced by the
original code (at least three of the list of six types) represent
bugs waiting to be found;  go through the code and check for all
of them, at least for the ones that can be located easily.  Add
these error classes to the list of potential problems checked
during code review.

I acknowledge that this response isn't exactly an answer to the
original question.  It does illustrate though a kind of thinking
that can be useful when trying to track down hard-to-find bugs.

Date Sujet#  Auteur
11 Jun 25 * Memory protection between compilation units?53Mateusz Viste
11 Jun 25 +* Re: Memory protection between compilation units?6Josef Möllers
12 Jun 25 i+* Re: Memory protection between compilation units?2Michael S
13 Jun 25 ii`- Re: Memory protection between compilation units?1Mateusz Viste
12 Jun 25 i`* Re: Memory protection between compilation units?3Richard Heathfield
16 Jun 25 i `* Re: Memory protection between compilation units?2Rosario19
16 Jun 25 i  `- Re: Memory protection between compilation units?1Richard Heathfield
11 Jun 25 +- Re: Memory protection between compilation units?1Lew Pitcher
11 Jun 25 +* Re: Memory protection between compilation units?3David Brown
12 Jun 25 i`* Re: Memory protection between compilation units?2Mateusz Viste
12 Jun 25 i `- Re: Memory protection between compilation units?1David Brown
11 Jun 25 +* Re: Memory protection between compilation units?3Opus
11 Jun 25 i+- Re: Memory protection between compilation units?1wij
12 Jun 25 i`- Re: Memory protection between compilation units?1Mateusz Viste
11 Jun 25 +- Re: Memory protection between compilation units?1Kaz Kylheku
12 Jun 25 +* Re: Memory protection between compilation units?21Mateusz Viste
12 Jun 25 i`* Re: Memory protection between compilation units?20Kaz Kylheku
13 Jun 25 i +* Re: Memory protection between compilation units?2Mateusz Viste
13 Jun 25 i i`- Re: Memory protection between compilation units?1Kaz Kylheku
13 Jun 25 i `* Re: Memory protection between compilation units?17pozz
13 Jun 25 i  `* Re: Memory protection between compilation units?16Mateusz Viste
13 Jun 25 i   +* Re: Memory protection between compilation units?13Michael S
13 Jun 25 i   i+* Re: Memory protection between compilation units?2Richard Heathfield
14 Jun 25 i   ii`- Re: Memory protection between compilation units?1Michael S
13 Jun 25 i   i+- Re: Memory protection between compilation units?1Kaz Kylheku
14 Jun 25 i   i`* Re: Memory protection between compilation units?9Mateusz Viste
15 Jun 25 i   i `* Re: Memory protection between compilation units?8Waldek Hebisch
15 Jun 25 i   i  `* Re: Memory protection between compilation units?7Mateusz Viste
16 Jun 25 i   i   `* Re: Memory protection between compilation units?6Waldek Hebisch
16 Jun 25 i   i    +* Re: Memory protection between compilation units?3Kaz Kylheku
16 Jun 25 i   i    i`* Re: Memory protection between compilation units?2Waldek Hebisch
16 Jun 25 i   i    i `- Re: Memory protection between compilation units?1James Kuyper
16 Jun 25 i   i    `* Re: Memory protection between compilation units?2Tim Rentsch
16 Jun 25 i   i     `- Re: Memory protection between compilation units?1Waldek Hebisch
13 Jun 25 i   +- Re: Memory protection between compilation units?1Kaz Kylheku
13 Jun 25 i   `- Re: Memory protection between compilation units?1wij
12 Jun 25 +* Re: Memory protection between compilation units?2Mikko
12 Jun 25 i`- Re: Memory protection between compilation units?1Mateusz Viste
12 Jun 25 +* Re: Memory protection between compilation units?10Tim Rentsch
13 Jun 25 i+* Re: Memory protection between compilation units?5Bonita Montero
13 Jun 25 ii`* Re: Memory protection between compilation units?4wij
13 Jun 25 ii +- Re: Memory protection between compilation units?1Bonita Montero
13 Jun 25 ii `* Re: Memory protection between compilation units?2Keith Thompson
13 Jun 25 ii  `- Re: Memory protection between compilation units?1Tim Rentsch
13 Jun 25 i`* Re: Memory protection between compilation units?4Mateusz Viste
14 Jun 25 i `* Re: Memory protection between compilation units?3Tim Rentsch
14 Jun 25 i  `* Re: Memory protection between compilation units?2Mateusz Viste
1 Jul17:54 i   `- Re: Memory protection between compilation units?1Tim Rentsch
13 Jun 25 +* Re: Memory protection between compilation units?3Bonita Montero
13 Jun 25 i`* Re: Memory protection between compilation units?2Mateusz Viste
13 Jun 25 i `- Re: Memory protection between compilation units?1Kaz Kylheku
16 Jun 25 +- Re: Memory protection between compilation units?1Mateusz Viste
21 Jun 25 `- Re: Memory protection between compilation units?1olcott

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal