Can I accidentally harm system files or personal f

2019-09-21 16:51发布

问题:

I do apologize for the lack of knowledge in this matter but it is very important for me, so at least I'll give it a try. I am using Visual Studio 2012 express on Windows 7 ultimate, I have written some very basic a silly program to practice structures in the C language. Unfortunately, one of the methods that I've written had a wrong condition to stop and it goes to an infinite loop and the worse part it exceeds the given array boundaries. I will add the code below BUT THE CODE IS NOT my main concern tho,I managed to fix it already, I only wish to know if I could harm my system or my personal files (videos, photos, office files etc.) with the given method because when I've tried to run this code, a scary thing happened, my pc started to "Beep" endlessly every 0.5 second and I could't do nothing to stop that, the program was on infinite loop and it I managed to stop this "fun" only with Ctrl + Alt + Del. I had written codes that crashed many times before, but I'ts sure the first time my pc strated to sound beeps. I hope an expert can help me out on this one. Many thanks.

The method with the infinite loop that cause all the problem is "printArrHero":

#include<stdio.h>  
#include<string.h>


struct Date{

    int day, month, year;

} typedef date_s;

struct Superhero{

    char name[30];

    double power, speed;

    date_s birthday;

} typedef sp_s;


void printInfo(sp_s hero){

    printf("Super hero info:\nName:  [%s]\n" , hero.name);

    printf("Power: [%.2lf]\n" , hero.power);

    printf("Speed: [%.2lf]\n" , hero.speed);

    printf("Birthday: [%d/%d/%d]\n\n" , hero.birthday.day, hero.birthday.month, hero.birthday.year );

}


void addHero (sp_s arr[], int *k, sp_s newHero){

    arr[*k] = newHero;
    (*k)++;
    printf("\nSuccess! a new superhero is added.\n"); 
}

void printArrHero (sp_s arr[]){

    int j=0;

    while ( arr[j] != NULL ){

        printf("[#%d]>>>>>>>>>>>>>>>>>>\n" , j+1); 

        printInfo( arr[j] );

        j++;
    }


}

void main(){

    sp_s myHeros[100];
    sp_s newHero;

    int i = -1;
    int k = 0;

    while (i != 0){ // menu loop
        printf("Welcome to my game!\n"); 
        printf("To add a hero press 1\n");
        printf("To find you're strongest hero press 2\n");
        printf("To find you're fastest hero press 3\n");
        printf("To see all of you're heroes list 4\n");
        printf("To exit press 0\n");
        scanf("%d" , &i);

        switch (i){
        case 0:
            break;

        case 1:
            printf("Please enter the #%d hero characteristics:\nName: " , k+1);
            scanf("%s" , &newHero.name);
            printf("Power: ");
            scanf("%lf" , &newHero.power);
            printf("Speed: ");
            scanf("%lf" , &newHero.speed);
            printf("Birth date [xx/xx/xx](format): ");
            scanf("%d/%d/%d" , &newHero.birthday.day, &newHero.birthday.month, &newHero.birthday.year);

            addHero(myHeros, &k, newHero); // now when I collecten all the info needed to make a new hero lets send it to the func tht puts it in the #k place in the array
            break;

        case 2:

            break;
        case 3:

            break;

        case 4:
            printArrHero(myHeros);
            break;

        } // END of switch-case

    } // while menu loop


} // main

回答1:

if I could harm my personal files (videos, photos, office files etc.)

Windows programs run in the context of the current user, and can perform any action of the current user. This means it could edit and delete your personal files.

Generally for a program to go rogue, it would need to have the activity within it. A program which deletes files, can more easily start accidentally deleting files. A program which edits files can make dodgy edits.

This is not a pre-requisite, as once it stops executing outside of your design, anything could happen.

if I could harm my system?

For your system to be harmed, you would need to be running as administrator. That gives the program the ability to do any action which an adminstator could do, which could include serious damage to your machine.

There is one further stage, which is system, which has complete control on the machine, more than an administator.

Is it likely?

Whilst it is possible, it is not very likely. The likelihood for modern programs is they crash before they corrupt files other than they are currently opening, however there are things which you can do to make the damage less-likely or more limited.

  • If you have significant files for your user, then consider switching to a different account for development. This would ring-fence the damage an errant program would cause.

  • Ensure you don't run with User-access-protection turned off. This limits your programs ability to administrate your machine.

  • Consider an alternative platform, the raspberry-pi, or Intel compute stick offer a cheap platform which can be re-built if things go wrong.

What happens in this case?

Assuming it gets stuck in a loop, without properly reading data (probably scanf() returning 0).

The array gets filled up with all 100 elements, and then starts corrupting the stack.

There are 2 plausible memory layouts (windows x86/x86-64).

+---------+
| return  |
|   from  |
| main    |
+---------+
|hero[99] |
+---------+
|hero[98] |
+---------+
|hero[97] |
+---------+
| ...     |

and

+---------+
| return  |
|   from  |
| main    |
+---------+
|  k      |
+---------+
|hero[99] |
+---------+
|hero[98] |
+---------+
|hero[97] |
+---------+
| ...     |

In the first case, the array gets over-run, and the return address is modified. There is code built into normal compiles (security cookie), which would detect this, and the result is a crash before bad stuff happens.

In the second case, the memory for variable k gets overwritten with one of the array writes. This then sends k to some kooky place, and random memory is overwritten. This could corrupt the program data, but most likely the first attempt at non-natural k (outside of 0-99) would result in a crash.

The likely failure was that you printed out a control code such as a beep - that causes strange behavior, and is consistent with what you saw - I don't think anything bad happened for you.