From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-yb1-xb31.google.com (mail-yb1-xb31.google.com [IPv6:2607:f8b0:4864:20::b31]) by sourceware.org (Postfix) with ESMTPS id 1A8803858C27 for ; Fri, 11 Mar 2022 19:01:18 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 1A8803858C27 Received: by mail-yb1-xb31.google.com with SMTP id w16so18726971ybi.12 for ; Fri, 11 Mar 2022 11:01:18 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=dK9c9s+MgQldPh7cUQ3WpIo6O7hQa480E2j8Jk7SIe4=; b=AyQfDl2LXtvgxNWUoTb6imAyse9qFjOMaQ8bX1k0ZtXUoLUcaxW8Y9C/lPStSnhSFf s6SMMk63p7482F3WavX2llW8vYFfCyn8qZ6ezJ2tMrB0Q5Ux5fVoecIKPgFaMeGBrsYV TrISra29aQfxB4Ksjnd9dLHJb1f/vCaecrJcLZxgwmD3L2E9vv5w0U9nd6sudrVHVMWI eKjmCJ/helZiYYP6UhYOsZXWOGY+qgppSGi4Xutgz2zm449Hf2mU0Dez0eNcciM5/BR5 Sb+DmFQC6LX9J5TlBzHc9vcvmJQQ9g5CyG/lunAPTGUgN3YzDOmoGgZePoBgzAH9Hpzp g/OQ== X-Gm-Message-State: AOAM531yTiJC3FH4fJecHBTBDQ8NPBDtkGZtn4dSM6VrYzH5/Gw3NvEj etTyP/Kb81HAwpPLg4XgNLk8VS934llqYMrS8kAY1xrH X-Google-Smtp-Source: ABdhPJwT54FZCZ5JhDnGDJH0yTttvG/OclcuvnmBEapWS5C8RkYvEgU3a5M1ZToOcNHlRQtrFtws5IXdHiwRY/ZlDHo= X-Received: by 2002:a25:ca4d:0:b0:629:7c93:cf82 with SMTP id a74-20020a25ca4d000000b006297c93cf82mr9369582ybg.358.1647025277109; Fri, 11 Mar 2022 11:01:17 -0800 (PST) MIME-Version: 1.0 References: <87o82ok616.fsf@oldenburg.str.redhat.com> <2a574a7a0dbb4f1ef4472d2a43b029f0ceca9065.camel@mengyan1223.wang> <204cd10c9d34c6c798e701f1d990c120c2d77897.camel@mengyan1223.wang> In-Reply-To: From: Reinoud Koornstra Date: Fri, 11 Mar 2022 11:01:05 -0800 Message-ID: Subject: Re: Fortify_source and stack-protector-strong To: Xi Ruoyao Cc: Florian Weimer , Reinoud Koornstra via Gcc-help X-Spam-Status: No, score=0.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, HTML_MESSAGE, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org Content-Type: text/plain; charset="UTF-8" X-Content-Filtered-By: Mailman/MimeDel 2.1.29 X-BeenThere: gcc-help@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-help mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 11 Mar 2022 19:01:19 -0000 One more question, for FORTIFY_SOURCE=2, can I use either -O1 or -O2 optimization flags? I noticed it doesn't like -Os. Thanks, Reinoud. On Wed, Mar 2, 2022, 12:45 PM Reinoud Koornstra wrote: > Thanks for the explanation! > Better to use =2 then. > But Strack protector and FORTIFY can be active as compiler arguments at > the same time as I understand. Just the binary size will grow. Thanks, > > Reinoud. > > On Wed, Mar 2, 2022, 12:23 PM Xi Ruoyao wrote: > >> On Wed, 2022-03-02 at 12:05 -0800, Reinoud Koornstra wrote: >> > Hi Xi, >> > >> > Thanks for your reply. >> > Then what is the difference between -D_FORTIFY_SOURCE=1 and >> -D_FORTIFY_SOURCE=2 exactly? >> >> -D_FORTIFY_SOURCE=1 uses __builtin_object_size(..., 0) as the buffer >> size, but -D_FORTIFY_SOURCE=2 uses __builtin_object_size(..., 1). Read >> https://gcc.gnu.org/onlinedocs/gcc/Object-Size-Checking.html for the >> details. >> >> One case is: >> >> struct frame >> { >> int size; >> char buf[0]; >> }; >> >> union >> { >> struct frame f; >> char padding[100 + sizeof(struct frame)]; >> } u; >> >> u.frame.size = strlen(s) + 1; >> strcpy(u.frame.buf, s); >> >> -D_FORTIFY_SOURCE=2 will abort this, but -D_FORTIFY_SOURCE=1 won't. >> (Yes, I know "char buf[0]" should be changed to a flexible array member >> "char buf[]" to fix this, but it is just an example.) >> -- >> Xi Ruoyao >> School of Aerospace Science and Technology, Xidian University >> >