メインコンテンツ

CERT C: Rule MSC41-C

Never hard code sensitive information

説明

ルール定義

Never hard code sensitive information.1

Polyspace 実装

ルール チェッカーは、"ハードコードされた機密データ" をチェックします。

すべて展開する

ハードコードされた機密データは、機密性の高いデータが文字列リテラルなどとしてコード内で直接露出された場合に発生します。チェッカーは、データを機密性が高いとして識別し、パスワード暗号化関数などの特定の関数で使用されないようにします。

以下のデータは機密性が高い可能性があります。

データの型情報の機密性を示唆する関数
ホスト名
  • sethostnamesetdomainnamegethostbynamegethostbyname2getaddrinfogethostbyname_rgethostbyname2_r (文字列引数)

  • inet_atoninet_ptoninet_net_ptoninet_addrinet_network (文字列引数)

  • mysql_real_connect mysql_real_connect_nonblockingmysql_connect (第 2 引数)

パスワード
  • CreateProcessWithLogonWLogonUser (第 1 引数)

  • mysql_real_connect mysql_real_connect_nonblockingmysql_connect (第 3 引数)

データベース
  • MySQL: mysql_real_connect mysql_real_connect_nonblockingmysql_connect (第 4 引数)

  • SQLite: sqlite3_opensqlite3_open16sqlite3_open_v2 (第 1 引数)

  • PostgreSQL: PQconnectdb

  • Microsoft SQL:SQLDriverConnect (第 3 引数)

ユーザー名
  • getpw, getpwnam, getpwnam_r, getpwuid, getpwuid_r

ソルトcryptcrypt_r (第 2 引数)
暗号化キーと初期化ベクトル

OpenSSL:

  • EVP_CipherInitEVP_EncryptInitEVP_DecryptInit (第 3 引数)

  • EVP_CipherInit_exEVP_EncryptInit_exEVP_DecryptInit_ex (第 4 引数)

シード
  • srandsrandominitstate (第 1 引数)

  • OpenSSL: RAND_seedRAND_add

リスク

ハードコードされた情報は、コードから生成されたバイナリから問い合わせることができます。

修正方法

機密情報のハードコーディングを回避します。

例 – 文字列リテラルを介して露出された機密データ
// Typically, you include the header "mysql.h" with function and type declarations.
// In this example, only the required lines from the header are quoted.

typedef struct _MYSQL MYSQL;

MYSQL *mysql_real_connect(MYSQL *mysql,
                          const char *host, const char *user, const char *passwd,
                          const char *db, unsigned int port, const char *unix_socket,
                          unsigned long client_flag);

typedef void * DbHandle;
extern MYSQL *sql;

// File that uses functions from "mysql.h" 
const char *host = "localhost";
char *user = "guest";
char *passwd;

DbHandle connect_to_database_server(const char *db)
{
    passwd = (char*)"guest";
    return (DbHandle)
        mysql_real_connect (sql, host, user, passwd, db, 0, 0x0, 0); //Noncompliant
}

この例では、mysql_real_connect 引数の host (ホスト名)、user (ユーザー名)、および passwd (パスワード) が文字列リテラルであり、コードに直接露出されます。

ASCII 文字列の生成されたバイナリを問い合わせると、この情報が公開されます。

修正 – 保護された構成ファイルから機密データを読み取る

1 つの修正方法として、構成ファイルからデータを読み取ります。次の修正例では、関数 connect_to_database_server_init を呼び出すことによって、保護された構成ファイルからホスト名、ユーザー名、およびパスワードがその引数に読み取られる可能性があります。

// Typically, you include the header "mysql.h" with function and type declarations.
// In this example, only the required lines from the header are quoted.

typedef struct _MYSQL MYSQL;

MYSQL *mysql_real_connect(MYSQL *mysql,
                          const char *host, const char *user, const char *passwd,
                          const char *db, unsigned int port, const char *unix_socket,
                          unsigned long client_flag);

typedef void * DbHandle;
extern MYSQL *sql;

// File that uses functions from "mysql.h" 

DbHandle connect_to_database_server(const char *db)
{
    const char *host_from_cfg;
    const char *user_from_cfg;
    const char *passwd_from_cfg;
    const char *db_from_cfg;
    if (connect_to_database_server_init(&host_from_cfg,
                                        &user_from_cfg,
                                        &passwd_from_cfg,
                                        &db_from_cfg))
    {
        return (DbHandle)
            mysql_real_connect (sql, host_from_cfg, user_from_cfg, 
                           passwd_from_cfg, db_from_cfg,  0, 0x0, 0);
    }
    else
        return (DbHandle)0x0;
}

チェック情報

グループ: Rule 48.その他 (MSC)

バージョン履歴

R2020a で導入


1 This software has been created by MathWorks incorporating portions of: the “SEI CERT-C Website,” © 2017 Carnegie Mellon University, the SEI CERT-C++ Web site © 2017 Carnegie Mellon University, ”SEI CERT C Coding Standard – Rules for Developing safe, Reliable and Secure systems – 2016 Edition,” © 2016 Carnegie Mellon University, and “SEI CERT C++ Coding Standard – Rules for Developing safe, Reliable and Secure systems in C++ – 2016 Edition” © 2016 Carnegie Mellon University, with special permission from its Software Engineering Institute.

ANY MATERIAL OF CARNEGIE MELLON UNIVERSITY AND/OR ITS SOFTWARE ENGINEERING INSTITUTE CONTAINED HEREIN IS FURNISHED ON AN "AS-IS" BASIS. CARNEGIE MELLON UNIVERSITY MAKES NO WARRANTIES OF ANY KIND, EITHER EXPRESSED OR IMPLIED, AS TO ANY MATTER INCLUDING, BUT NOT LIMITED TO, WARRANTY OF FITNESS FOR PURPOSE OR MERCHANTABILITY, EXCLUSIVITY, OR RESULTS OBTAINED FROM USE OF THE MATERIAL. CARNEGIE MELLON UNIVERSITY DOES NOT MAKE ANY WARRANTY OF ANY KIND WITH RESPECT TO FREEDOM FROM PATENT, TRADEMARK, OR COPYRIGHT INFRINGEMENT.

This software and associated documentation has not been reviewed nor is it endorsed by Carnegie Mellon University or its Software Engineering Institute.