My friend Simon talked with me, he would like to buy some instant coffee or ice cream in supermarket, but he found that, all of them including one ingredient is non-dairy creamer(植脂末). This ingredient is used to make the drink more smooth, it’s a replacement of milk, but it’s bad for human health. Any way, it’s better than melamine(三聚氰胺). Nowadays, you are hard to find instant coffee without non-dairy in China.
Another story what I heard there is a government department named as “temporary emergency processing center for cover of sewer well” (窨井盖临时应急处理中心) is hiring a fixed officer. And as government perspective view, this department will be always existed even the name including “temporary”, the reason is they have to alwyas handle the “temporary” & “emergency” issue. But as I know, the cover of sewer well shall be managed by municipal administration.
The third story is restrict the car to solve the traffic jam, i.e. if the last digit of your car license plate number is 1 or 9, your car will be ban on road on Monday, 2 or 8 will be ban on road on Tuesday, and so on…. This is a temporary solution for traffic jam issue, but currently, it’s becoming a final solution, it’s normal for administration department.
So we can understand from those stories, once problem happen, there will be temporary solution coming, in programming area, we called “workaround”, and there is no further analysis, and temporary solution will act as final solution, workaround will act as solution. Once there is another problem happen, there will be another temporary solution coming, or another “workaround” be used. A workaround helps to solve another workaround, and another workaround is used to solve another another workaround problem….
But why? I often ask this question to myself and my colleague. It might be caused by lazy, yes, that’s one reason. But another problem in government administration area, the temporary solution can solve some problems of employment, and there will be more position for officer ask, even it’s not needed. In software programming area as well, more workaround, more maintenance is needed, and more engineers are needed. But all of us forget one thing is find out the root cause, and fix it.
If exception has been normal, around of you use workaround solve another problem of workaround, it means the situation around of you has been worse, you need to consider quit only if you can solve it. :\
We produce telecom equipment, the equipments will be deployed as infrastructure of telecommunication, internet etc., so it required product high stability, high security. We would not like to see there will be a 911 phone call cannot be reachable since our software bug, or financial exchange be stuck since networking problem which is caused by our software bug.
I recall that one day, Erno Jeges (who is secure coding expert) told us: you have to care your code as care your baby, otherwise you don’t know what your baby will be. So that, we have to deal software security carefully: buffer overflow, print formatting, and integer overflow etc. I’m not expert in this area, but I notice that at least we have to take care of our integer variable first since C/C++ languages handle integers in a very dangerous manner since there are:
No overflow exception
No run-time detection if a negative integer is converted to an unsigned value
No checks whether a larger integer value is put into a shorter variable
That’s why in our source code hides one integer number multiply by one integer number, and leads software fiery crash. Of cause, sometimes, we can rely on static analysis tool such as Klocwork, cppcheck, but not all of problems can be filtered by tool, human shall participate in the process of software creation. As these reason, programmer who is programming in C/C++ language shall be carefully deal with the calculation or convention of integer variable, and it shall be basic knowledge of C/C++ programmer. That’s why I act Longevity Monk who from A Chinese Odyssey here.
It’s time to start the puzzle game, please spot out the bomb which is hiden inside the code, let’s look at this code snippet:
1234567891011
intcopy(char*src,intlen){chardest[50];if(len>50){return-1;}memcpy(dest,src,len);//TODO: use dest herereturn0;}
Could you please tell me is there any problem if you stop here and do not plan to go through this post? Of cause, there is a big problem once assign a negative value to parameter len, for example, we assign the len is -1, the program can be executed in memcpy(dest, src, len), and -1 will be casted to unsigned value 0xffffffff, and lead the program Segmentation fault, this problem is called Signedness bug.
Okay, let’s see another code snippet here:
123456789101112
intappend(char*str1,char*str2,unsignedintlen1,unsignedintlen2){chardest_buf[64];if((len1+len2)>64){return-1;}memcpy(dest_buf,str1,len1);memcpy(dest_buf+len1,str2,len2);//TODO: dest_buf usage can be added herereturn0;}
How about this one, is there any problem? In case of len1 is huge number, and len1+len2 can be overflow to a small number. For example, len1 = 4294967198, and len2 = 100, and then len1 + len2 = 4294967298 (0x100000002). We call this problem is Arithmetical overflow.
one more, this will be easily to spot with previous experience:
12345678910111213141516
#include <stdio.h>#include <string.h>intcopy(char*src,intlen){unsignedshorts;charbuf[80];s=len;if(s>=80){printf("string is too long!\n");return-1;}memcpy(buf,src,len);buf[len]='\0';return0;}
If the len is 65535, the s will be 0, and lead segmentation fault during memcpy, we call it Widthness integer overflow. So here we would like to highlight the ranges of integer, we believe professional programmer can deal it well.
And now it’s time to review you owned source code, is there any same problem? Have you group the integer calculation invoking in some centralized place rather than spread everywhere of your program? Have you clearly understand using variable is 2bytes, 4bytes or 8bytes? signed or unsigned?
Be careful! :\
(Knowledge I learned from Computer Systems: A programmer’s perspective, and emphasize the mindset in secure coding course, if you forget the knowledge, hope this post can remind you pick your book up.)