Pavel
But visual code and code blocks dont
Vlad
It is C++
ClassRoom R[] = {{1, randomSize, randomSize},
{2, randomSize, randomSize},
{3, randomSize, randomSize},
{4, randomSize, randomSize}};
Here is what I am working with
These can't be moved. Elements would live till the end of scope
Anonymous
Anonymous
also destructors are mainly used to free any allocations made in the constructor or lifetime of the object
Anonymous
mainly RAII
Anonymous
yea
Anonymous
and technically you can be filling it with zero
Vlad
Vlad
If you store by value best you can do is to set up a flag of some kind that it's in fact empty
Anonymous
~m() {
// erase contents incase someone posses our pointer
memset(rooms, 0, room_len);
// erase our pointer incase someone trues to use us
rooms = nullptr;
}
Anonymous
assuming rooms itself is not allocated
Vlad
Anonymous
otherwise
~m() {
// erase contents incase someone posses our pointer
memset(rooms, 0, room_len);
// erase our pointer incase someone trues to use us
delete rooms;
rooms = nullptr;
}
Vlad
Vlad
Not like you're gonna access it upon deletion or something
Vlad
And after free anything could happen to a buffer
Anonymous
eg an attacker/malisious lib/program may try to read contents of memory after it has been freed
Vlad
Vlad
So it'd be recognizable
Anonymous
memset 0 is sufficent
Anonymous
since the pattern COULD be decoded eventually, compromizing your data
Vlad
Vlad
If it's 0xFDFDFDFD or smth
Anonymous
(tho in that case, simply attempting to read data WHILE your class is active is enough to compromise it as any such memory read attack would be)
Alviro Iskandar
Alviro Iskandar
you need a memory barrier
Anonymous
true
Anonymous
Anonymous
tons of valuable info could be gained if kernel memory could be read via an exploit by an attacker
Anonymous
and they where persistant enough and knew what to look for
Vlad
I'm pretty sure that is a headache of the OS devs
Anonymous
:)
Vlad
What to do with memory after free
Vlad
So it wouldn't be recognizable
Alviro Iskandar
Alviro Iskandar
in the linux kernel we have kfree_sensitive() to zero the memory before free
Anonymous
you might be able to use a custom deleter to memset it, or a hardened malloc/free/new/delete implementation specifically designed for such
Alviro Iskandar
like private key in memory, etc.
Alviro Iskandar
freeing doesn't always clobber the content, so it may reside in memory
Vlad
Alviro Iskandar
https://github.com/torvalds/linux/blob/v5.18/mm/slab_common.c#L1234-L1255
Anonymous
such as the one used in GraphineOS
Anonymous
https://github.com/GrapheneOS/hardened_malloc i think it is
Anonymous
CONFIG_ZERO_ON_FREE: true (default) or false to control whether small allocations are zeroed on free, to mitigate use-after-free and uninitialized use vulnerabilities along with purging lots of potentially sensitive data from the process as soon as possible. This has a performance cost scaling to the size of the allocation, which is usually acceptable. This is not relevant to large allocations because the pages are given back to the kernel.
Anonymous
also https://github.com/GrapheneOS/hardened_malloc#security-properties
Pavel
I think even if it changes the behaviour it can skip writing to the memory (if it stored value in a register), but not sure if this really can happen
Anonymous
anyway
Anonymous
how do i detect overflow in a subtraction operation?
eg
public int Add(int x, int y)
{
int r = x + y;
int s = (x >> 31);
fOK &= s != (y >> 31) || s == (r >> 31);
return r;
}
public long Add(long x, long y)
{
long r = x + y;
long s = (x >> 63);
fOK &= s != (y >> 63) || s == (r >> 63);
return r;
}
public uint Add(uint x, uint y)
{
uint r = x + y;
fOK &= (r >= x);
return r;
}
public ulong Add(ulong x, ulong y)
{
ulong r = x + y;
fOK &= r >= x;
return r;
}
Alviro Iskandar
Vlad
Vlad
After the destructor is called the object is toast
Alviro Iskandar
Vlad
You can't access it anyway
Vlad
Vlad
Latter is obviously not portable
Anonymous
without using any builtins or assembly or casting
Anonymous
as for example
public long Add(long x, long y)
{
long r = x + y;
long s = (x >> 63);
fOK &= s != (y >> 63) || s == (r >> 63);
return r;
}
this should be reversable/flippable for a Sub equivilant, right?
Vlad
Vlad
Vlad
But it would only work if both are positive
Anonymous
eg int64_t + int64_t
Vlad
Vlad
I guess you can check int64_t through a double variable
Vlad
Hence it's range is bigger
Vlad
But there might be accuracy issues idk
Anonymous
also wont be as fast
Anonymous
depending on hardware
Vlad
Vlad
FPU pipeline works almost concurrently
Anonymous
eg, for int64_t + int64_t we do
// assuming sizeof(long) == sizeof(int64_t)
long r = x + y;
long s = (x >> 63);
fOK &= s != (y >> 63) || s == (r >> 63); // fOK = !overflowed
Anonymous
and for unsigned equivilant we simply do
ulong r = x + y;
fOK &= r >= x;
Anonymous
hmmm i wonder if this would fail if x and y where flipped