Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[-Wunsafe-buffer-usage] Accept calls to some libc functions with annotated arguments #10088

Open
wants to merge 2 commits into
base: next
Choose a base branch
from

Conversation

ziqingluo-90
Copy link

  • printf, fprintf snprintf functions accept __null_terminated
  • snprintf function accepts __counted_by/__sized_by
  • functions consuming a single string pointer like strlen or atoi accept __null_terminated

Generalized isCountAttributedPointerArgumentSafe so that it is shared by interoperation gadgets and the unsafe libc gadget.

(A follow-up change to rdar://138798346)

…tated arguments

- `printf`, `fprintf` `snprintf` functions accept `__null_terminated`
- `snprintf` function accepts `__counted_by/__sized_by`
- functions consuming a single string pointer like `strlen` or `atoi`
  accept `__null_terminated`

Generalized `isCountAttributedPointerArgumentSafe` so that it is
shared by interoperation gadgets and the unsafe libc gadget.

(A follow-up change to rdar://138798346)
@ziqingluo-90
Copy link
Author

CC @dtarditi

@ziqingluo-90
Copy link
Author

This PR depends on #10060

@@ -747,7 +747,7 @@ const Expr *extractExtentFromSubviewDataCall(ASTContext &Context,
static bool hasIntegeralConstant(const Expr *E, uint64_t Val, ASTContext &Ctx) {
Expr::EvalResult ER;

if (E->EvaluateAsConstantExpr(ER, Ctx)) {
if (E->EvaluateAsInt(ER, Ctx)) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to https://github.com/llvm/llvm-project/pull/124022/files, the result of EvaluateAsConstantExpr may not necessarily be able to getInt() later.

@@ -23,7 +23,7 @@ namespace std {
T* p;
T *c_str();
T *data();
unsigned size_bytes();
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A mistake I made in the test---std::string has no size_bytes method.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's probably worth checking if basic_string<wchar_t> works as expected.

@@ -366,6 +366,7 @@ isInUnspecifiedUntypedContext(internal::Matcher<Stmt> InnerMatcher) {

namespace {

/* TO_UPSTREAM(BoundsSafetyInterop) ON */
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume there is no problem in upstreaming the part of the interop in UnsafeBufferUsage.cpp? @patrykstefanski

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, this is fine.

@ziqingluo-90
Copy link
Author

add @jkorous-apple

snwprintf(safe_p, n, "%s", str); // expected-warning{{function 'snwprintf' is unsafe}} expected-note{{string argument is not guaranteed to be null-terminated}}

memcpy(safe_p, safe_p, n); // no warn
strlen(str); // expected-warning{{unsafe assignment to a parameter of '__null_terminated' type; only '__null_terminated' pointers, string literals, and 'std::string::c_str' calls are compatible with '__null_terminated' pointers}}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not getting this warning here.

error: 'expected-warning' diagnostics expected but not seen:
  File ./clang/test/SemaCXX/warn-unsafe-buffer-usage-libc-functions-interop.cpp Line 36: unsafe assignment to a parameter of '__null_terminated' type; only '__null_terminated' pointers, string literals, and 'std::string::c_str' calls are compatible with '__null_terminated' pointers

&*ValuesOpt, Context);
return isCountAttributedPointerArgumentSafeImpl(
Context, Arg, CountArg, CAT, CAT->getCountExpr(), CAT->isCountInBytes(),
CAT->isOrNull(), ValuesOpt ? &*ValuesOpt : nullptr);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: IMHO we could be explicit to avoid any confusion: ValuesOpt.has_value() ? &*ValuesOpt : nullptr.

assert(CountedByExpr ||
!DependentValueMap &&
"If the __counted_by information is hardcoded, there is no "
"dependent value map.");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the operator precedence in those 2 asserts correct? The bottom one is equivalent to:

CountedByExpr ||
         (!DependentValueMap &&
             "If the __counted_by information is hardcoded, there is no "
             "dependent value map.")

Is this expected?

auto ValuesOpt = getDependentValuesFromCall(CAT, Call);
if (!ValuesOpt.has_value())
if (!DependentValueMap && CountedByExpr)
// Bail if the map is not available for case (a). Becuase

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Becuase -> Because.

return false;

const Expr *ArgCount = nullptr;
// the acutal count of the pointer inferred through patterns below:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

acutal -> actual


// expected-note@+2{{consider using a safe container and passing '.data()' to the parameter 'dst' and '.size()' to its dependent parameter 'size' or 'std::span' and passing '.first(...).data()' to the parameter 'dst'}}
// expected-note@+1{{consider using a safe container and passing '.data()' to the parameter 'src' and '.size()' to its dependent parameter 'size' or 'std::span' and passing '.first(...).data()' to the parameter 'src'}}
void memcpy(void * __sized_by(size) dst, const void * __sized_by(size) src, unsigned size);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

memcpy returns void * and takes size_t size.

void memcpy(void * __sized_by(size) dst, const void * __sized_by(size) src, unsigned size);
unsigned strlen( const char* __null_terminated str );
// expected-note@+1{{consider using a safe container and passing '.data()' to the parameter 'buffer' and '.size()' to its dependent parameter 'buf_size' or 'std::span' and passing '.first(...).data()' to the parameter 'buffer'}}
int snprintf( char* __counted_by(buf_size) buffer, unsigned buf_size, const char* format, ... );

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Those printf functions take size_t.

// expected-note@+1{{consider using a safe container and passing '.data()' to the parameter 'buffer' and '.size()' to its dependent parameter 'buf_size' or 'std::span' and passing '.first(...).data()' to the parameter 'buffer'}}
int snprintf( char* __counted_by(buf_size) buffer, unsigned buf_size, const char* format, ... );
int snwprintf( char* __counted_by(buf_size) buffer, unsigned buf_size, const char* format, ... );
int vsnprintf( char* __counted_by(buf_size) buffer, unsigned buf_size, const char* format, ... );

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should take va_list vlist instead of ....

}
return true; // ptr and size are not in safe pattern
return !isHardcodedCountedByPointerArgumentSafe(
Finder->getASTContext(), Buf, Size, FirstParmTy.getTypePtr(), true,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is true passed to isSizedBy correct here? I think the functions taking wchar_t would be annotated with __counted_by().

@@ -23,7 +23,7 @@ namespace std {
T* p;
T *c_str();
T *data();
unsigned size_bytes();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's probably worth checking if basic_string<wchar_t> works as expected.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants