メインコンテンツ

MISRA C:2023 Rule 21.21

The Standard Library function system of <stdlib.h> shall not be used

R2024a 以降

説明

ルール定義

The Standard Library function system of <stdlib.h> shall not be used 1 .

This rule comes from MISRA C™:2012 Amendment 2.

根拠

関数 system の引数がサニタイズされていない場合、悪用可能な脆弱性の原因になる可能性があります。攻撃者は任意のコマンドを実行したり、システム上の任意の場所にあるデータを読み取ったり変更したりできます。

Polyspace 実装

チェッカーは、標準ライブラリ関数 system の使用にフラグを設定します。

トラブルシューティング

ルール違反を想定していてもその違反が表示されない場合、コーディング規約違反が想定どおりに表示されない理由の診断を参照します。

すべて展開する

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

enum { 
SIZE512=512,
SIZE3=3};

void func_noncompliant(char *arg)
{
    char buf[SIZE512];
    int retval=sprintf(buf, "/usr/bin/any_cmd %s", arg);

    if (retval<=0 || retval>SIZE512){
      /* Handle error */
      abort();
    }
    /* Use of system() to pass any_cmd with 
    unsanitized argument to command processor */

    if (system(buf) == -1) { //Noncompliant
    /* Handle error */
  }
} 

void func_compliant(char *arg)
{
    char *const args[SIZE3] = {"any_cmd", arg, NULL};
    char  *const env[] = {NULL}; 
  
    /* Sanitize argument */
  
    /* Use execve() to execute any_cmd. */

    if (execve("/usr/bin/time", args, env) == -1) { //Compliant
      /* Handle error */
    }
} 

この例では、関数 func_noncompliant 内で、実行するコマンド プロセッサのホスト環境に関数 system が引数を渡しています。このコードはコマンド インジェクションによる攻撃に対して脆弱です。

同じ関数の準拠するバージョン func_compliant では、any_cmd の引数がサニタイズされた後に関数 execve に渡されて実行されます。exec ファミリ関数は、コマンド インジェクション攻撃に対して脆弱ではありません。

チェック情報

グループ: 標準ライブラリ
カテゴリ: 必要
AGC カテゴリ: 必要

バージョン履歴

R2024a で導入


1 All MISRA coding rules and directives are © Copyright The MISRA Consortium Limited 2021.

The MISRA coding standards referenced in the Polyspace® Bug Finder™ documentation are from the following MISRA standards:

  • MISRA C:2004

  • MISRA C:2012

  • MISRA C:2023

  • MISRA C++:2008

  • MISRA C++:2023

MISRA and MISRA C are registered trademarks of The MISRA Consortium Limited 2021.