Skip to content

Commit 5439c02

Browse files
committed
CHB: add precondition for restricted output-format string
1 parent 8b821cc commit 5439c02

11 files changed

Lines changed: 214 additions & 7 deletions

‎CodeHawk/CHB/bchlib/bCHAttributesFunctionSemantics.ml‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,24 @@ let get_output_format_string_precondition
315315
attrparams_to_string "argparams" argparams]
316316

317317

318+
let get_restricted_output_format_string_precondition
319+
(fparams: fts_parameter_t list)
320+
(tagparams: b_attrparam_t list)
321+
(argparams: b_attrparam_t list): xxpredicate_t traceresult =
322+
match (tagparams, argparams) with
323+
| ([], (AInt refindex) :: rest)
324+
when List.for_all (function AStr _ -> true | _ -> false) rest
325+
&& rest <> [] ->
326+
let* par = get_par fparams refindex in
327+
let specs = List.map (function AStr s -> s | _ -> assert false) rest in
328+
Ok (XXRestrictedOutputFormatString (ArgValue par, specs))
329+
| _ ->
330+
Error [(elocm __LINE__) ^ "restricted_output_format_string params not recognized";
331+
fparams_to_string fparams;
332+
attrparams_to_string "tagparams" tagparams;
333+
attrparams_to_string "argparams" argparams]
334+
335+
318336
let get_trusted_os_cmd_fmt_string_precondition
319337
(fparams: fts_parameter_t list)
320338
(tagparams: b_attrparam_t list)
@@ -556,6 +574,20 @@ let get_chk_pre_conditions
556574
end)
557575
(get_output_format_string_precondition fparams tagparams argparams)
558576

577+
| (ACons ("restricted_output_format_string", tagparams)) :: argparams ->
578+
TR.tfold
579+
~ok:(fun xpre -> [xpre])
580+
~error:(fun e ->
581+
begin
582+
log_error_result
583+
~tag:"get_chk_preconditions:restricted_output_format_string"
584+
~msg:name
585+
__FILE__ __LINE__
586+
[String.concat ", " e];
587+
[]
588+
end)
589+
(get_restricted_output_format_string_precondition fparams tagparams argparams)
590+
559591
| (ACons ("trusted_os_cmd_fmt_string", tagparams)) :: argparams ->
560592
TR.tfold
561593
~ok:(fun xpre -> [xpre])

‎CodeHawk/CHB/bchlib/bCHCallSemanticsRecorder.ml‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
open CHNumerical
3131

3232
(* chutil *)
33+
open CHFormatStringParser
3334
open CHLogger
3435
open CHPrettyUtil
3536

@@ -59,6 +60,27 @@ let log_error (tag: string) (msg: string): tracelogspec_t =
5960
mk_tracelog_spec ~tag:("callsemanticsrecorder:" ^ tag) msg
6061

6162

63+
let specifier_of_conversion (c: conversion_t): string =
64+
match c with
65+
| StringConverter -> "s"
66+
| IntConverter | DecimalConverter -> "d"
67+
| UnsignedDecimalConverter -> "u"
68+
| UnsignedOctalConverter -> "o"
69+
| UnsignedHexConverter false -> "x"
70+
| UnsignedHexConverter true -> "X"
71+
| FixedDoubleConverter false -> "f"
72+
| FixedDoubleConverter true -> "F"
73+
| ExpDoubleConverter false -> "e"
74+
| ExpDoubleConverter true -> "E"
75+
| FlexDoubleConverter false -> "g"
76+
| FlexDoubleConverter true -> "G"
77+
| HexDoubleConverter false -> "a"
78+
| HexDoubleConverter true -> "A"
79+
| UnsignedCharConverter -> "c"
80+
| PointerConverter -> "p"
81+
| OutputArgument -> "n"
82+
83+
6284
class call_semantics_recorder_t
6385
(loc: location_int)
6486
(finfo: function_info_int)
@@ -153,6 +175,32 @@ object (self)
153175
else
154176
Open
155177
| _ -> Open)
178+
| XPORestrictedOutputFormatString (x, sl) ->
179+
(match x with
180+
| XConst (IntConst n) ->
181+
let dw = numerical_mod_to_doubleword n in
182+
if string_table#has_string dw then
183+
let s = string_table#get_string dw in
184+
let fmtspec = CHFormatStringParser.parse_formatstring s false in
185+
let actual =
186+
List.map
187+
(fun a -> specifier_of_conversion a#get_conversion)
188+
fmtspec#get_arguments in
189+
if actual = sl then
190+
Discharged (
191+
"format specifiers ["
192+
^ (String.concat "," sl)
193+
^ "] match: " ^ s)
194+
else
195+
Violated (
196+
"format specifiers ["
197+
^ (String.concat "," actual)
198+
^ "] do not match required ["
199+
^ (String.concat "," sl)
200+
^ "]: " ^ s)
201+
else
202+
Open
203+
| _ -> Open)
156204
| XPOAllocationBase x ->
157205
(match x with
158206
| XVar v when self#finfo#env#is_return_value v ->

‎CodeHawk/CHB/bchlib/bCHExternalPredicate.ml‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,10 @@ let rec xxpredicate_compare (p1: xxpredicate_t) (p2: xxpredicate_t): int =
157157
| (XXOutputFormatString t1, XXOutputFormatString t2) -> btc t1 t2
158158
| (XXOutputFormatString _, _) -> -1
159159
| (_, XXOutputFormatString _) -> 1
160+
| (XXRestrictedOutputFormatString (t1, sl1), XXRestrictedOutputFormatString (t2, sl2)) ->
161+
let l0 = btc t1 t2 in if l0 = 0 then Stdlib.compare sl1 sl2 else l0
162+
| (XXRestrictedOutputFormatString _, _) -> -1
163+
| (_, XXRestrictedOutputFormatString _) -> 1
160164
| (XXPositive t1, XXPositive t2) -> btc t1 t2
161165
| (XXPositive _, _) -> -1
162166
| (_, XXPositive _) -> 1
@@ -242,6 +246,7 @@ let rec xxpredicate_terms (p: xxpredicate_t): bterm_t list =
242246
| XXNonNegative t -> [t]
243247
| XXNullTerminated t -> [t]
244248
| XXOutputFormatString t -> [t]
249+
| XXRestrictedOutputFormatString (t, _) -> [t]
245250
| XXPositive t -> [t]
246251
| XXRelationalExpr (_, t1, t2) -> [t1; t2]
247252
| XXStartsThread (t, tt) -> t :: tt
@@ -392,6 +397,13 @@ let rec xxpredicate_to_pretty (p: xxpredicate_t) =
392397
| XXNonNegative t -> default "non-negative" [t]
393398
| XXNullTerminated t -> default "null-terminated" [t]
394399
| XXOutputFormatString t -> default "output-format-string" [t]
400+
| XXRestrictedOutputFormatString (t, sl) ->
401+
LBLOCK [
402+
STR "output-format-string-restricted(";
403+
btp t;
404+
STR ", [";
405+
STR (String.concat "," sl);
406+
STR "])"]
395407
| XXPositive t -> default "positive" [t]
396408
| XXRelationalExpr (op, t1, t2) ->
397409
LBLOCK [btp t1; STR (relational_op_to_string op); btp t2]
@@ -465,6 +477,7 @@ let rec modify_types_xxp
465477
| XXPositive t -> XXPositive (mbt t)
466478
| XXNullTerminated t -> XXNullTerminated (mbt t)
467479
| XXOutputFormatString t -> XXOutputFormatString (mbt t)
480+
| XXRestrictedOutputFormatString (t, sl) -> XXRestrictedOutputFormatString (mbt t, sl)
468481
| XXRelationalExpr (op, t1, t2) -> XXRelationalExpr (op, mbt t1, mbt t2)
469482
| XXStartsThread (t, tt) -> XXStartsThread (mbt t, List.map mbt tt)
470483
| XXTainted t -> XXTainted (mbt t)

‎CodeHawk/CHB/bchlib/bCHFloc.ml‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -797,16 +797,16 @@ object (self)
797797
~tag:"update_arm_varargs:no format arg info available"
798798
~msg:self#cia
799799
__FILE__ __LINE__
800-
["argxpr: " ^ (x2s argxpr)] in
800+
["argxpr: " ^ (x2s (XVar v))] in
801801
None)
802802
| _ ->
803803
let _ =
804804
log_diagnostics_result
805805
~tag:"update_arm_varargs:unable to externalize argument"
806806
~msg:self#cia
807807
__FILE__ __LINE__
808-
["argxpr: " ^ (x2s argxpr)];
809-
None)
808+
["argxpr: " ^ (x2s (XVar v))] in
809+
None)
810810
| Some argxpr ->
811811
let _ =
812812
log_diagnostics_result

‎CodeHawk/CHB/bchlib/bCHFunctionPODischarge.ml‎

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
open CHLanguage
3030

3131
(* chutil *)
32+
open CHFormatStringParser
3233
open CHLogger
3334

3435
(* xprlib *)
@@ -507,6 +508,96 @@ let discharge_output_format_string
507508
status
508509

509510

511+
(* -------------------------------------------------------------------------
512+
XPORestrictedOutputFormatString
513+
------------------------------------------------------------------------- *)
514+
515+
let specifier_of_conversion (c: conversion_t): string =
516+
match c with
517+
| StringConverter -> "s"
518+
| IntConverter | DecimalConverter -> "d"
519+
| UnsignedDecimalConverter -> "u"
520+
| UnsignedOctalConverter -> "o"
521+
| UnsignedHexConverter false -> "x"
522+
| UnsignedHexConverter true -> "X"
523+
| FixedDoubleConverter false -> "f"
524+
| FixedDoubleConverter true -> "F"
525+
| ExpDoubleConverter false -> "e"
526+
| ExpDoubleConverter true -> "E"
527+
| FlexDoubleConverter false -> "g"
528+
| FlexDoubleConverter true -> "G"
529+
| HexDoubleConverter false -> "a"
530+
| HexDoubleConverter true -> "A"
531+
| UnsignedCharConverter -> "c"
532+
| PointerConverter -> "p"
533+
| OutputArgument -> "n"
534+
535+
536+
let restricted_output_format_string_constant_string_stmt
537+
(loc: location_int) (x: xpr_t) (sl: string list): po_status_t =
538+
match get_constant_string loc x with
539+
| Some s ->
540+
let fmtspec = parse_formatstring s false in
541+
let actual =
542+
List.map
543+
(fun a -> specifier_of_conversion a#get_conversion)
544+
fmtspec#get_arguments in
545+
if actual = sl then
546+
Discharged (
547+
"format specifiers ["
548+
^ (String.concat "," sl)
549+
^ "] match: " ^ s)
550+
else
551+
Violated (
552+
"format specifiers ["
553+
^ (String.concat "," actual)
554+
^ "] do not match required ["
555+
^ (String.concat "," sl)
556+
^ "]: " ^ s)
557+
| _ -> Open
558+
559+
560+
let restricted_output_format_string_delegate
561+
(finfo: function_info_int)
562+
(loc: location_int)
563+
(x: xpr_t)
564+
(sl: string list): po_status_t =
565+
match x with
566+
| XVar v when finfo#env#is_initial_register_value v ->
567+
TR.tfold
568+
~ok:(fun reg ->
569+
let _ =
570+
finfo#add_register_parameter_location reg BCHBCTypeUtil.t_charptr 4 in
571+
let ftspar = finfo#get_summary#get_parameter_for_register reg in
572+
let dst = ArgValue ftspar in
573+
let xpred = XXRestrictedOutputFormatString (dst, sl) in
574+
let _ = finfo#add_precondition xpred in
575+
Delegated xpred)
576+
~error:(fun e ->
577+
begin
578+
log_error_result
579+
~tag:"discharge_restricted_output_format_string"
580+
~msg:(p2s loc#toPretty)
581+
__FILE__ __LINE__
582+
["v: " ^ (p2s v#toPretty); String.concat ", " e];
583+
Open
584+
end)
585+
(finfo#env#get_initial_register_value_register v)
586+
| _ ->
587+
Open
588+
589+
590+
let discharge_restricted_output_format_string
591+
(finfo: function_info_int)
592+
(loc: location_int)
593+
(x: xpr_t)
594+
(sl: string list): po_status_t =
595+
let status = restricted_output_format_string_constant_string_stmt loc x sl in
596+
match status with
597+
| Open -> restricted_output_format_string_delegate finfo loc x sl
598+
| _ -> status
599+
600+
510601
(* -------------------------------------------------------------------------
511602
XPOBuffer / XPOBlockWrite (stack size check)
512603
------------------------------------------------------------------------- *)
@@ -1063,6 +1154,9 @@ let discharge_one
10631154
| XPOOutputFormatString x ->
10641155
discharge_output_format_string finfo po#loc x
10651156

1157+
| XPORestrictedOutputFormatString (x, sl) ->
1158+
discharge_restricted_output_format_string finfo po#loc x sl
1159+
10661160
| XPOBlockWrite (ty, x, bwlen) ->
10671161
discharge_blockwrite finfo po#loc ty x bwlen
10681162

‎CodeHawk/CHB/bchlib/bCHInterfaceDictionary.ml‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,8 @@ object (self)
561561
| XXNonNegative t -> (tags, [it t])
562562
| XXNullTerminated t -> (tags, [it t])
563563
| XXOutputFormatString t -> (tags, [it t])
564+
| XXRestrictedOutputFormatString (t, sl) ->
565+
(tags, (it t) :: (List.map bd#index_string sl))
564566
| XXPositive t -> (tags, [it t])
565567
| XXRelationalExpr (op, t1, t2) ->
566568
(tags @ [relational_op_mfts#ts op], [it t1; it t2])
@@ -622,6 +624,8 @@ object (self)
622624
| "nng" -> XXNonNegative (gt (a 0))
623625
| "nt" -> XXNullTerminated (gt (a 0))
624626
| "ofs" -> XXOutputFormatString (gt (a 0))
627+
| "rofs" ->
628+
XXRestrictedOutputFormatString (gt (a 0), List.map bd#get_string (List.tl args))
625629
| "pos" -> XXPositive (gt (a 0))
626630
| "x" -> XXRelationalExpr (relational_op_mfts#fs (t 1), gt (a 0), gt (a 1))
627631
| "st" -> XXStartsThread (gt (a 0), List.map gt (List.tl args))

‎CodeHawk/CHB/bchlib/bCHLibTypes.mli‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2412,6 +2412,9 @@ type xxpredicate_t =
24122412
| XXNullTerminated of bterm_t (** term points to null-terminated string *)
24132413
| XXOutputFormatString of bterm_t
24142414
(** term points to format string for output (e.g., sprintf) *)
2415+
| XXRestrictedOutputFormatString of bterm_t * string list
2416+
(** term points to a restricted output format string; the string list gives
2417+
the allowed format specifiers in order (e.g., ["s";"s";"s";"s"]) *)
24152418

24162419
| XXRelationalExpr
24172420
of relational_op_t * bterm_t * bterm_t (** relational expression *)
@@ -2651,6 +2654,7 @@ type xpo_predicate_t =
26512654
| XPOPositive of xpr_t
26522655
| XPONullTerminated of xpr_t
26532656
| XPOOutputFormatString of xpr_t
2657+
| XPORestrictedOutputFormatString of xpr_t * string list
26542658
| XPORelationalExpr of relational_op_t * xpr_t * xpr_t
26552659
| XPOStartsThread of xpr_t * xpr_t list
26562660
| XPOTainted of xpr_t

‎CodeHawk/CHB/bchlib/bCHSumTypeSerializer.ml‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,7 @@ object
472472
| XXPositive _ -> "pos"
473473
| XXNullTerminated _ -> "nt"
474474
| XXOutputFormatString _ -> "ofs"
475+
| XXRestrictedOutputFormatString _ -> "rofs"
475476
| XXRelationalExpr _ -> "x"
476477
| XXStartsThread _ -> "st"
477478
| XXTainted _ -> "t"
@@ -487,7 +488,7 @@ object
487488
method !tags = [
488489
"ab"; "b"; "bw"; "con"; "dis"; "e"; "fp"; "fr"; "ga";
489490
"ha"; "i"; "ifs"; "inc"; "inv"; "ir"; "m";
490-
"nm"; "nn"; "nng"; "no"; "nt"; "nu"; "nz"; "ofs";
491+
"nm"; "nn"; "nng"; "no"; "nt"; "nu"; "nz"; "ofs"; "rofs";
491492
"pos"; "sa"; "st"; "t"; "tc"; "tfa"; "tfs"; "ts"; "v"; "wfs"; "x"
492493
]
493494

@@ -527,6 +528,7 @@ object
527528
| XPOPositive _ -> "pos"
528529
| XPONullTerminated _ -> "nt"
529530
| XPOOutputFormatString _ -> "ofs"
531+
| XPORestrictedOutputFormatString _ -> "rofs"
530532
| XPORelationalExpr _ -> "x"
531533
| XPOStartsThread _ -> "st"
532534
| XPOTainted _ -> "t"
@@ -542,7 +544,7 @@ object
542544
method !tags = [
543545
"ab"; "b"; "bw"; "con"; "dis"; "e"; "fp"; "fr"; "ga";
544546
"ha"; "i"; "ifs"; "inc"; "inv"; "ir"; "m";
545-
"nm"; "nn"; "nng"; "no"; "nt"; "nu"; "nz"; "ofs";
547+
"nm"; "nn"; "nng"; "no"; "nt"; "nu"; "nz"; "ofs"; "rofs";
546548
"pos"; "sa"; "st"; "t"; "tc"; "tfa"; "tfs"; "ts"; "v"; "wfs"; "x"
547549
]
548550

‎CodeHawk/CHB/bchlib/bCHVersion.ml‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,8 @@ end
9595

9696

9797
let version = new version_info_t
98-
~version:"0.6.0_20260611"
99-
~date:"2026-06-11"
98+
~version:"0.6.0_20260614"
99+
~date:"2026-06-14"
100100
~licensee: None
101101
~maxfilesize: None
102102
()

‎CodeHawk/CHB/bchlib/bCHXPODictionary.ml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,8 @@ object (self)
120120
| XPOPositive x -> (tags, [ix x])
121121
| XPONullTerminated x -> (tags, [ix x])
122122
| XPOOutputFormatString x -> (tags, [ix x])
123+
| XPORestrictedOutputFormatString (x, sl) ->
124+
(tags, (ix x) :: (List.map bd#index_string sl))
123125
| XPORelationalExpr (op, x1, x2) ->
124126
(tags @ [relational_op_mfts#ts op], [ix x1; ix x2])
125127
| XPOStartsThread (x, xl) -> (tags, (ix x) :: (List.map ix xl))

0 commit comments

Comments
 (0)