Is allocating objects from a memory-pool a security anti-pattern?

In the wake of the heartbleed bug, OpenSSL has been rightly critizised for using its own freelist. With plain-old malloc, the bug would have almost certainly been found long ago.

However, some kind of malloc wrapping is actually very common. For example when I have a big object that owns many small, fixed sized objects, I often allocate them from an array.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>/* BigObj is allocated with `malloc`, but LittleObj using `alloc_little`. */
typedef struct {
/* bigObj stuff here */
int nlittle;
LittleObj littles[MAX_LITTLE];
} BigObj;
LittleObj *alloc_little(BigObj *big)
{
if(big->nlittle == MAX_LITTLE)
return NULL;
return = big->littles + big->nlittle++;
}
</code>
<code>/* BigObj is allocated with `malloc`, but LittleObj using `alloc_little`. */ typedef struct { /* bigObj stuff here */ int nlittle; LittleObj littles[MAX_LITTLE]; } BigObj; LittleObj *alloc_little(BigObj *big) { if(big->nlittle == MAX_LITTLE) return NULL; return = big->littles + big->nlittle++; } </code>
/* BigObj is allocated with `malloc`, but LittleObj using `alloc_little`. */
typedef struct {

    /* bigObj stuff here */

    int nlittle;
    LittleObj littles[MAX_LITTLE];
} BigObj; 

LittleObj *alloc_little(BigObj *big)
{
    if(big->nlittle == MAX_LITTLE)
        return NULL;
    return = big->littles + big->nlittle++;
}

alloc_little is faster than malloc(), but the main reason is simplicity. There is no free_little to call, because the LittleObjs just remain valid until the BigObj is destroyed.

So is this an anti-pattern? If so, can I mitigate the risks, or should I just abandon it? Similar questions go for any kind of memory pool, such as GNU Obstacks and the talloc used in Samba.

No, absolutely not. And proposing malloc() as the solution is just terrible.

Complex pieces of system software almost always have special memory requirements, and very often the solution involves implementing one or more customer memory allocators. Reusing objects off a free list is one good strategy. Others include memory pools, slab allocators, stack allocators, reference counting and even garbage collection.

C/C++ happens to be an exceptionally fine tool for implementing memory allocators, but it provides very little protection from bad decisions or bad execution. The mistake in OpenSSL was not the decision to use a free list, but doing it so badly.

I would say the main risk mitigation strategy, after making good choices and writing good code, is exhaustive unit testing, including failure modes and performance. Don’t leave home without it.

No.

C is not safe. C is not friendly. C does not catch your errors for you. As you say, using a different allocator might have caught the error sooner – but it would not have prevented it in the first place, at the design stage. The same or a similar error would likely still have been made, and while other strategies might have helped catch it sooner, it would still be after the fact.

The antipattern is in forgetting this and designing code that can potentially overflow or make invalid accesses in the first place, that doesn’t either check what it’s being asked to do or prove it won’t do anything dangerous at compile-time. So there is an antipattern here if you just “don’t track” a LittleObj: deal with this by being able to prove where their owners are at all times (and that it’s mechanically impossible for the owner of a LittleObj to outlive its associated pool), not by hoping undefined behaviour will point out a mistake later.

Ruling this sort of technique out as an antipattern makes it less easy to use C for one of its strengths: implementing low-level, unsafe, runtime details so that higher-level languages and systems can be built on top of it (not every GC or other runtime can be built on malloc effectively). If in doubt, better not to be using C for the complex high-level stuff at all – leave it to an altogether safer system rather than trying to hammer C into a tool it isn’t, and let a machine prove or ensure that your program is safe (stricter programming language, restrictive API, machine-checkable spec, etc.). Otherwise what would we implement low-level features in?

The ultimate counterexample is probably the ML Kit, whose C-implemented backend uses regions – a much more advanced form of the same basic idea as pools – combined with the ironclad SML type system to prevent invalid memory accesses.

2

of cause, it’s not an anti-pattern, it’s just one more way to fire in your leg
it can be usefull or can be not usefull, depending on usage scenario
there are a lot of technics to use statically pre-allocate memory (and not use malloc/free) and such technics often use in embedded devices
but, using such technics you should clearly understand will be possible to expose data to another user of your application. for example, if you use pre-allocated memory to dram primitives on screen – it’s generally safe, if you use such memory to return signed (by key stored in some crypto storage) data – you have to wipe data before returning memory record to user.
I mean, then someone calls your alloc_little() you returns some memory and it can contains previous data. If your code doesn’t operate with some sensitive data (it’s really very hard to specify what IS a sensitive data) – you can don’t clean the memory. If you think, data operated inside LittleObj is sensitive – you have to clear this memory before usage.
Moreover, you have to track all allocated memorries, because on can call alloc_little()/free_little() and keep address of struct. and periodically poll it for information.
SO, it’s not an antipattern, it’s veru usefull technic on some cases but you should clearly understand what do you do and who can access this memory.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật